简体   繁体   中英

JS: How to create a file of type image, from base64

I'm trying to convert png file, produced by https://html2canvas.hertzen.com/ to a blob file, that I'd send to API.

Code below produces such output that API does not throw 400, however the file is somehow corrupted.

Is there something wrong with the way I construct the blob?

  const data = new FormData();
  const [, binary] = image.toDataURL().split(',');
  const blobFile = new Blob([window.atob(binary)], { type: 'image/png' });
  data.append('attachments[]', blobFile, 'screenshot.png');

Alright, turns out canvas is already well equipped for translating itself to blob. All you have to do is use canvas.toBlob(cb) and you are ready to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM