使用javascript将base64string转换为可下载文件
我想在点击链接的同时下载文件使用javascript将base64string转换为可下载文件,javascript,base64,Javascript,Base64,我想在点击链接的同时下载文件 <a href="javascript:download(375,untitled.png,image/x-png,*base64bytestring*)" >untitled.png</a> 您可以使用这个简单的下载功能,将url、所需文件名和类型传递给它 function download(url, filename, mimeType){ return (fetch(url) .then(fu
<a href="javascript:download(375,untitled.png,image/x-png,*base64bytestring*)" >untitled.png</a>
您可以使用这个简单的下载功能,将url、所需文件名和类型传递给它
function download(url, filename, mimeType){
return (fetch(url)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], filename, {type:mimeType});})
);
}
download('data:text/plain;base64,aGVsbG8gd29ybGQ=', 'hello.txt', 'text/plain')
.then(function(file){
console.log(file);
})
我建议你们使用图书馆。包括此库,然后使用图像mime类型尝试以下代码:
<a href="javascript:download(atob('*base64bytestring*'), 'dlText.txt', 'text/plain');" >untitled.txt</a>
<a href="javascript:download(atob('*base64bytestring*'), 'dlText.txt', 'text/plain');" >untitled.txt</a>