解决浏览器自动打开文件而不是下载

2020-07-24  本文已影响0人  Moruin

var a = document.createElement('a');

a.setAttribute('href', url);

a.setAttribute('download', url.split('/').pop()); //分割路径,取出最后一个元素

a.setAttribute('target', '_blank');

a.setAttribute('id', 'DownloadFile');// 防止反复添加

if(document.getElementById('DownloadFile')) {       

    document.body.removeChild(document.getElementById('DownloadFile'));

}

document.body.appendChild(a);

a.click();

上一篇 下一篇

猜你喜欢

热点阅读