我試圖使用http鏈接下載文件。 這是代碼:下載文本文件或圖像在Firefox上拋出JavaScript
downloadFile: function (fileName, url) {
var link = document.createElement('a');
link.setAttribute('download', fileName);
link.setAttribute('type', 'application/octet-stream');
link.target = '_blank';
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
這適用於Chrome和邊緣,但不是在Firefox瀏覽器。 當我試圖下載一個文本文件或圖像時,瀏覽器會打開一個新選項卡來顯示文件內容。我需要瀏覽器打開默認的下載窗口。
可能嗎?
這是URL的一個例子,我使用:http://localhost:17671/docstmp/528d149e37467a53faeeeb0556901d87/ToDo.txt
我創造了這個的jsfiddle證明:jsfiddle.net/hp7yod85
豈不正常的下載鏈接工作的結果呢? – Jite
包含'link.setAttribute('type','application/octet-stream')的目的是什麼? link.target ='_blank';'? – guest271314
沒有目標,文件內容與我的應用程序在同一頁面中打開。 並且該類型是嘗試下載的測試 –