0

I have an Image object

   var ctx = canvas.getContext('2d');
   ctx.drawImage(imageBitmap, 0, 0);
   var imgData=ctx.getImageData(0,0,W,H);

How can I now pass imgData as binary data (or a string) to the server using POST method?

1

1 Answer 1

0

First, you must get byte array of the image. It's easy:

let bytes = imgData.data

Then, send bytes to server. It's simple too:

fetch('https://example.net/',{method: 'POST', body: bytes}).then((res) => ...)

But, as a matter of fact, their is very few server which receive raw image data. In reality, send processed data such as jpeg compression and base64 encoding.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, right... I've been trying to send imgData instead of imgData.data. It seems it works with xmlhttprequest as well. Thanks.