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