Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// sending-a-file-as-multipart-through-xmlhttprequest
https://stackoverflow.com/questions/9395911/sending-a-file-as-multipart-through-xmlhttprequest
// change the id `USE-YOUR-ID`
const img = document.getElementById('USE-YOUR-ID');
// extracts image data from <img> element through canvas
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0 );
const myData = context.getImageData(0, 0, img.width, img.height);
// this converts the image into base64
const str = btoa(myData.data)
console.log(str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.