Creating a BLOB from a Base64 string in JavaScript

后端 未结 12 2624
说谎
说谎 2020-11-21 04:24

I have Base64-encoded binary data in a string:

const contentType = \'image/png\';
const b64Data = \'iVBORw0KGg         


        
12条回答
  •  野的像风
    2020-11-21 05:07

    If you can stand adding one dependency to your project there's the great blob-util npm package that provides a handy base64StringToBlob function. Once added to your package.json you can use it like this:

    import { base64StringToBlob } from 'blob-util';
    
    const contentType = 'image/png';
    const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
    
    const blob = base64StringToBlob(b64Data, contentType);
    
    // Do whatever you need with your blob...
    

提交回复
热议问题