I'm trying to send an image from the server to the client. After some googling, the best solution seems to send the data as ArrayBuffer and then convert it to Blob on FE. However, I'm not able to get the image to display on FE. Am I doing some conversions wrong that might cause the issue?
For the server code:
exports.getImage = async (req, res) => {
try {
const file = fs.readFileSync(`${__dirname}/images/image.png`);
let ab = file.buffer.slice(file.byteOffset, file.byteOffset + file.byteLength);
return res.status(200).send(ab);
} catch (err) {
console.log(err);
return res.status(400).send({
code: err.errCode,
description: 'General error'
});
}
}
And on the receiving side in angular:
service.ts
getCustomMap(groupId: string, mapId: string): Observable<ArrayBuffer> {
return this._http
.get(`${this._URL}/${groupId}/customMap/${mapId}`, {
responseType: 'arraybuffer'
});
}
image component:
getCustomMap() {
this._groupManagerService.getCustomMap()
.subscribe((imgFile: ArrayBuffer) => {
map.file = new Blob([imgFile], { type: 'image/png' });
map.url = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(map.file));
});
}
Thank you