vue下载后端传回的二进制文件

    downloadFile(file) {
      this.axios({
        url: "/downUrl/downloadFile",  //后端的下载地址
        method: "post",
        responseType: "blob",
        data: {
          filePath: file.file_url
        }
      }).then(resp => {
        let objectUrl = URL.createObjectURL(resp);   // 创建url对象用来下载
        this.myDownloadFile(objectUrl, file.file_name)
      });
    },
    myDownloadFile(content, filename) {           //下载方法(传入url对象和文件名)
      let a = document.createElement('a')
      document.body.appendChild(a)
      a.href = content
      a.download = filename
      a.click()
      a.remove()
    },

 

posted @ 2021-11-02 14:19  偏执狂傲  阅读(256)  评论(0)    收藏  举报