Answer the question
In order to leave comments, you need to log in
How to push an image through SignalR?
Hello.
I want to send an image from the server (on .net core 3.1), the usual jpg, via SignalR. Get it in the browser, in the angular project and display it on the page.
The usual text chat works for me.
What I do with the image:
1. On the server, I send the image to all (without bothering) clients, the image is in byte array format:
byte[] file = await File.ReadAllBytesAsync(@"C:\Avatars\user7.jpg");
await Clients.All.BroadCastImage(file);
// стандартные подключения
public initConnection() {
this.connection = new signalR.HubConnectionBuilder()
.withUrl('/chat')
.build();
}
public connectionWebSocket() {
this.connection.start();
this.connection.on("BroadCastImage", image => {
var blob = new Blob([image], { type: "image/jpeg" });
var reader = new FileReader();
reader.readAsDataURL(blob);
var that = this;
reader.onloadend = function () {
that.base64data = that.sanitizer.bypassSecurityTrustUrl(reader.result + '');
}
});
}
private sanitizer: DomSanitizer
<img [src]="base64data"/>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question