11.前台文件数据流下载获取源文件名

1.文件流下载文件

axios实现:

export function download(url, data){
    axios.post(baseurl+url,data,{responseType: 'blob'})
    .then(function (response) {
        var filename = response.headers//下载后文件名
        filename = filename["content-disposition"]
        filename = filename.split(";")[1].split("filename=")[1];
        var blob = new Blob([response.data])
          var downloadElement = document.createElement('a');
        var href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = filename
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象 
    })
    .catch(function (error) {
        console.log("File download error!")
    })
}

注意,想要获取headers中content-disposition的数据,服务端要加一个:

response.setHeader("Access-Control-Expose-Headers","Content-Disposition");

转载于:https://www.cnblogs.com/TimerHotel/p/vue_11.html

你可能感兴趣的:(11.前台文件数据流下载获取源文件名)