js 如何实现 base64 转文件下载保存到本地

function dataURLtoBlob(dataurl) {
    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 Blob([u8arr], { type: mime });
}
 
function downloadFile(url,name='What\'s the fuvk'){
    var a = document.createElement("a")
    a.setAttribute("href",url)
    a.setAttribute("download",name)
    a.setAttribute("target","_blank")
    let clickEvent = document.createEvent("MouseEvents");
    clickEvent.initEvent("click", true, true);  
    a.dispatchEvent(clickEvent);
}
 
function downloadFileByBase64(base64,name){
    var myBlob = dataURLtoBlob(base64)
    var myUrl = URL.createObjectURL(myBlob)
    downloadFile(myUrl,name)
}
 
downloadFileByBase64('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAACXBIWXMAAAsSAAALEgHS3X78AAAEk0lEQVRYhbVXS09bRxQ+N3bNRdjEai3UYiGMYdFFUKzykKoKSpWN1QU1YtVVCavu4vML4vQPDFGURRYosIGtQxeVF8hG2cGiICMUVUh1awlSQaldVLBsB1fncsaM773Dq80nWXPndeabOd85MzYajQbcBogYBIAYAGwKIUq3MsK442i5BhBxDgD+AoAslVy/NW58Eog4AwAvHR0AD4UQC47Wa+A2J5HgcgsA+rgkzDhGvkcSQeWbtLDpGHFDeHXDWXi06wiLL81dVH4JAPcBoAAAd7k9p8yd4XnUn75KuK6aYAI5XkhiSwgR437y/XdK3yshREI3DwAmLiOic8ecYmiNy/uImKQPIQTt9AW3vyAC/D3jNo/taaEjEZGHIoSYoJ1yPaGMeWsr1f5XPA9t9lyh1YTcGSKS8B5wvQAXPo+xMGNcT3M/6eUBIk5cN2J0JBYU8WWV9k8RkRbqVdq+4R8d+R63+W3zLnWHqzB5t+T/FKv/TwD4SOn+jQ1v8okkmLSEHF8mG0KI65FARGnMEVas+t8BIAAAiyxMB+lwOCympqagv79fJZtExJwS7mS7Jbd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传给你的是什么类型的文件

比如是 png 的话,上面代码里的 mime 就硬编码:image/png

如果是 gif 的话,上面代码里的 mime 就硬编码:image/gif

JS 转 Base64 之后的 data 类型方法如下:

复制代码

// JS转Base64之后的data类型
    getBase64Type(type) {
        switch (type) {
            case 'txt': return 'data:text/plain;base64,';
            case 'doc': return 'data:application/msword;base64,';
            case 'docx': return 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,';
            case 'xls': return 'data:application/vnd.ms-excel;base64,';
            case 'xlsx': return 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,';
            case 'pdf': return 'data:application/pdf;base64,';
            case 'pptx': return 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,';
            case 'ppt': return 'data:application/vnd.ms-powerpoint;base64,';
            case 'png': return 'data:image/png;base64,';
            case 'jpg': return 'data:image/jpeg;base64,';
            case 'gif': return 'data:image/gif;base64,';
            case 'svg': return 'data:image/svg+xml;base64,';
            case 'ico': return 'data:image/x-icon;base64,';
            case 'bmp': return 'data:image/bmp;base64,';
        }
    }

复制代码

获取文件后缀

复制代码

getType(file) {
        var filename = file;
        var index1 = filename.lastIndexOf(".");
        var index2 = filename.length;
        var type = filename.substring(index1 + 1, index2);
        return type;
    }

复制代码

请成为永远疯狂永远浪漫永远清澈的存在。

留下你的脚步
推荐阅读