/*防止下载txt文件直接打开而不是是下载*/

if(res.data.res == '1') {
setTimeout(() => {
this.loadingAdd.close()
}, 3000);
var index = res.data.file.lastIndexOf("//");
var fileName = res.data.file.substring(index + 2, res.data.file.length)

//window.location.href = 'http://www.380v.com' + '/storage/excel/' + fileName
/*防止下载txt文件直接打开而不是是下载*/
/*注意:本地运行,还是会直接打开,部署到服务器就好啦!*/
let fileUrl = 'http://www.380v.com' + '/storage/excel/' + fileName;
let fileNameArray = fileUrl.split('/');
let fileName = fileNameArray[fileNameArray.length - 1];
let element = document.createElement('a');
element.style.display = 'none';
element.setAttribute('href', fileUrl);
element.setAttribute('download', fileName);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
} else {
res.msg = '导出失败'
}

 

posted @ 2022-10-10 15:33  nnc  阅读(89)  评论(0编辑  收藏  举报