js将文件流转换成文件
这里转了有两种方法转换,代码如下:
export default {
//下载excel方法 支持IE与非IE
goDownloadExcel(ref){
if (ref.data) {
let lists = ref.data;
var fileName = decodeURIComponent(ref.headers['content-disposition'].split('=')[1]);
console.log(lists);
var blob = new Blob([lists], {type: 'application/vnd.ms-excel;charset=utf-8'});
if ('download' in document.createElement('a')) {//非IE下载
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对象
}else{ //IE 10+ 下载
navigator.msSaveBlob(blob,fileName);
}
}
},
//下载Word方法 支持IE与非IE
goDownloadWord(ref){
if (ref.data) {
let lists = ref.data;
var fileName = decodeURIComponent(ref.headers['content-disposition'].split('=')[1]);
console.log(lists);
var blob = new Blob([lists], {type: 'application/octet-stream;charset=utf-8'});
if ('download' in document.createElement('a')) {//非IE下载
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对象
}else{ //IE 10+ 下载
navigator.msSaveBlob(blob,fileName);
}
}
},
//模拟点击链接 下载excel
goDownloadExcelHref(ref){
var downloadElement = document.createElement('a');
downloadElement.href = ref.data;
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
// loadingInstance.close();
document.body.removeChild(downloadElement); //下载完成移除元素
// window.URL.revokeObjectURL(href); //释放掉blob对象
return this.$message.success("下载完成!");
}
}