Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript将base64string转换为可下载文件_Javascript_Base64 - Fatal编程技术网

使用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>