Saving base64 image with Filesaver.js

2020-03-05 03:22发布

问题:

I am getting multiple Base64 URIs of JPG images.

I need to save them as jpg files.

I'm trying to use FileSaver.js, but its not working for me.

I have used filesaver.js before, when I was getting images from aws-sdk, in which, the data was in buffer form and it worked.

However, its not working for me with base64 string.

var base64 = '/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gODUK/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJDBEPEhIRDxERExYcFxMUGhURERghGBodHR8fHxMXIiQiHiQcHh8e/9sAQwEFBQUHBgcOCAgOHhQRFB4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e/8AAEQgAYABgAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A+VFOG5HWvb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wtpng/QYdK0uB0iTJd5W3O7HqzHuTQ9XoNGtpQ1Boka8QRHaCUBzg45rN8X6j9ltw6vt5A+tamoXwhjPzKfxrjPEkd3quzKJFbxyCSSQtkBVOePrj9aJNJWGj5Z/bM1z7RfW+neWeH3bvw/8Ar1876UMsT716l+1RrK6l48MCEYiTJx/tHj9MV5ho/Kk5zya22SRJ/9k='

saveAs(new Blob([base64], {type: "image/jpeg"}),"filename.jpg")

I have also tried:

saveAs(new Blob([base64], {type: "image/jpeg;base64"}),"filename.jpg")

saveAs(new Blob([atob(base64)], {type: "image/jpeg"}),"filename.jpg")

I have also tried using data:image/jpeg;base64, as a suffix to my base64 string.

The file gets saved, but images are corrupted. In some cases, the files just contain the base64 string. How do I save them as binary files?

回答1:

I used the code below to convert my Data URI to BLOB, then use it with FileSaver.js:

function dataURItoBlob(dataURI) {
  // convert base64 to raw binary data held in a string
  // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
  var byteString = atob(dataURI.split(',')[1]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

  // write the bytes of the string to an ArrayBuffer
  var ab = new ArrayBuffer(byteString.length);

  // create a view into the buffer
  var ia = new Uint8Array(ab);

  // set the bytes of the buffer to the correct values
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }

  // write the ArrayBuffer to a blob, and you're done
  var blob = new Blob([ab], {type: mimeString});
  return blob;

}

I found this code at: Blob from DataURL?



回答2:

I had a similar issue with base64 pdfs, and I found this code on stackoverflow:

function b64toFile(b64Data, filename, contentType) {
    var sliceSize = 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];

    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);
        var byteNumbers = new Array(slice.length);

        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    var file = new File(byteArrays, filename, {type: contentType});
    return file;
}

This solved all my problems. Before, I was doing a similar method and my pdfs were all corrupted but after applying this code it worked. Hope it helps you. I am going to try to find the original answer where I found this code so that I can give proper credit.