JS 解決txt文件直接打開而不是下載


 

PS:后台傳過來的路徑不可以直接下載txt文件:先創建a標簽然后把路徑填入,download屬性一定要填上下載的文件的名稱,可以解決txt文件直接打開而不是下載

 

var url = "../../../Temp/14049234c3ef498583f9ff213b5b5529/D05B567C0602414D8CCFFDCA677DE060.txt";
//創建a標簽
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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM