vue 下载文件

excelExportTemplateBtn: function (){
const url=‘下载方法路径’
      
        this.getBlob(url).then(blob => {
          this.saveAs(blob, 'test.xlsx')
        })
}
		
getBlob: function (url, options = {}) {
        return new Promise(resolve => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', url, true)
        xhr.responseType = 'blob'
        xhr.onload = () => {
          if (xhr.status === 200) {
            resolve(xhr.response)
          }
        }

        xhr.send()
      })
      },
	  
 saveAs: function(blob, filename) {
        const blobURL = window.URL.createObjectURL(blob);
          const tempLink = document.createElement("a");
          tempLink.style.display = "none";
          tempLink.href = blobURL;
          tempLink.setAttribute("download", filename);
          if (typeof tempLink.download === "undefined") {
            tempLink.setAttribute("target", "_blank");
          }
          document.body.appendChild(tempLink);
          tempLink.click();
          document.body.removeChild(tempLink);
          window.URL.revokeObjectURL(blobURL);
      }

posted @ 2024-11-24 22:09  小海葵  阅读(13)  评论(0编辑  收藏  举报