Skip to content

Instantly share code, notes, and snippets.

@vsimko
Last active September 19, 2017 14:02
Show Gist options
  • Select an option

  • Save vsimko/6df81892c8f4e44df6e210f6ff3acc1a to your computer and use it in GitHub Desktop.

Select an option

Save vsimko/6df81892c8f4e44df6e210f6ff3acc1a to your computer and use it in GitHub Desktop.
// 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