I am trying to convert image
to byte array
using only javascript and send to api. I have found this answer but it only shows to convert to base64 then send it to server side. Are there any other ways to convert to byte array using js or I can't just send byte array to api ?
Asked
Active
Viewed 853 times
0

Kaung Khant Zaw
- 1,508
- 3
- 19
- 31
-
If you are just wanting to convert a file that you've uploaded into a byte array, you might be able to use the solution to this question: https://stackoverflow.com/questions/37134433/convert-input-file-to-byte-array – Sxribe Dec 28 '20 at 04:09
-
This all depends on how you're getting your input. Most of the time, you want a Uint8Array or ArrayBuffer to efficiently send your data to the API, but to get that you will need either FileReader.readAsArrayBuffer (if from a user upload) or CanvasRenderingContext2D.getImageData().data. See https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer and https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData. – 101arrowz Dec 28 '20 at 04:14