0

I hav e node express server with sends an image like res.sendFile(__dirname + '/image.png'). So it sends response with Content-Type: image/png.

I use xhr to receive this image. I am not setting responseType property to blob (it's important)!!

In this case xhr.response looks like the following:

�e+-M2����h��4�d&���|<��w�3�}Μ�9�|J.ݜ|���$P�L~f������z�I$�٭â :,�pI��b3[ /,=�����쐇̟n��=r���w�f��OM�lw(�k��� �0���Æ�ƈ��OD��ޑ�E�

How can I convert it to arraybuffer?

1 Answers1

0

I believe the problem comes in when the image/png data is converted to UTF-8, due to which it cannot be directly processed further, even into base64. If using XHR, the idea is to specify an expected response type as follows, so that it is not converted into an unwanted format:

let xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";

For more details, here is the reference: Answer by Alex K.

  • As I mentioned I can't use responseType property –  Jan 15 '21 at 12:27
  • What do you want to do with the image after fetching? Do you want to render it on the page using HTML? If so, how about setting the image URL instead? – ojaswa1942 Jan 15 '21 at 12:44
  • i want to display for example, or to send further - whatever. so i need it as an arraybuffer object –  Jan 15 '21 at 12:58