axios下载文件乱码问题 无法解压 文件损坏

/* 下载附件 */
        downloadFile(fileName) {
            // window.open(url);
            var that = this;
            var url = "PO2116"; //接口地址
            that.$http
                ({
                    url:url + "?filePath=" + fileName,
                    method: 'post',
                    headers:{
                        'Content-Type': 'application/json; application/octet-stream'
                    },
                    responseType: 'blob',
                })
                .then(res => {
                    console.log(res)
                    const blob = new Blob([res.data]);
                    const downloadElement = document.createElement("a");
                    const href = window.URL.createObjectURL(blob);
                    //后台再header中传文件名
                    const name = fileName;
                    downloadElement.href = href;
                    downloadElement.download = name;
                    document.body.appendChild(downloadElement);
                    downloadElement.click();
                    document.body.removeChild(downloadElement); // 下载完成移除元素
                    window.URL.revokeObjectURL(href); // 释放掉blob对象
 
 
                });
        },

  

这俩是重点

headers:{
           'Content-Type': 'application/json; application/octet-stream'
},
 responseType: 'blob',

 

posted @ 2019-09-18 17:24  前端王者(法师)  阅读(2102)  评论(0编辑  收藏  举报