将 base64 png 数据转换为 javascript 文件对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16968945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:37:56  来源:igfitidea点击:

Convert base64 png data to javascript file objects

javascriptbase64fileapi

提问by Bonik

I have two base64encoded in PNG, and I need to compare them using Resemble.JS

我有两个base64PNG 编码,我需要使用Resemble.JS比较它们

I think that the best way to do it is to convert the PNG's into file objects using fileReader. How can I do it?

我认为最好的方法是PNG使用fileReader. 我该怎么做?

采纳答案by c69

You can create a Blobfrom your base64 data, and then read it asDataURL:

您可以Blob从 base64 数据创建一个,然后读取它asDataURL

var img_b64 = canvas.toDataURL('image/png');
var png = img_b64.split(',')[1];

var the_file = new Blob([window.atob(png)],  {type: 'image/png', encoding: 'utf-8'});

var fr = new FileReader();
fr.onload = function ( oFREvent ) {
    var v = oFREvent.target.result.split(',')[1]; // encoding is messed up here, so we fix it
    v = atob(v);
    var good_b64 = btoa(decodeURIComponent(escape(v)));
    document.getElementById("uploadPreview").src = "data:image/png;base64," + good_b64;
};
fr.readAsDataURL(the_file);

Full example (includes junk code and console log): http://jsfiddle.net/tTYb8/

完整示例(包括垃圾代码和控制台日志):http: //jsfiddle.net/tTYb8/



Alternatively, you can use .readAsText, it works fine, and its more elegant.. but for some reason textdoes not sound right ;)

或者,您可以使用.readAsText,它工作正常,并且更优雅......但由于某种原因,文本听起来不正确;)

fr.onload = function ( oFREvent ) {
    document.getElementById("uploadPreview").src = "data:image/png;base64,"
    + btoa(oFREvent.target.result);
};
fr.readAsText(the_file, "utf-8"); // its important to specify encoding here

Full example: http://jsfiddle.net/tTYb8/3/

完整示例:http: //jsfiddle.net/tTYb8/3/

回答by cuixiping

Way 1: only works for dataURL, not for other types of url.

方式1:仅适用于dataURL,不适用于其他类型的url。

function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type:mime});
}

//Usage example:
var file = dataURLtoFile('data:image/png;base64,......', 'a.png');
console.log(file);

Way 2: works for any type of url, (http url, dataURL, blobURL, etc...)

方式 2:适用于任何类型的 url,(http url、dataURL、blobURL 等...)

//return a promise that resolves with a File instance
function urltoFile(url, filename, mimeType){
    mimeType = mimeType || (url.match(/^data:([^;]+);/)||'')[1];
    return (fetch(url)
        .then(function(res){return res.arrayBuffer();})
        .then(function(buf){return new File([buf], filename, {type:mimeType});})
    );
}

//Usage example:
urltoFile('data:image/png;base64,......', 'a.png')
.then(function(file){
    console.log(file);
})

Both works in Chrome and Firefox.

两者都适用于 Chrome 和 Firefox。

回答by love2code

Previous answer didn't work for me.

以前的答案对我不起作用。

But this worked perfectly. Convert Data URI to File then append to FormData

但这工作得很好。 将数据 URI 转换为文件,然后附加到 FormData