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

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

作者: Moruin | 来源:发表于2020-07-24 16:54 被阅读0次

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();

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/hgqslktx.html