首页 > Web开发 > 详细

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

时间:2020-02-12 18:51:44      阅读:1823      评论:0      收藏:0      [点我收藏+]
    

<img :src="后台返回的数据" >  

   download(){
   //this.base指的是后台返回的 
   //‘data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAAB
      
 downloadFileByBase64(this.base,‘ss‘)
      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)
        }
      },

 

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

原文:https://www.cnblogs.com/xqhv587/p/12300050.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!