[英]How to download a file without the “Leave the Page” dialog or any other prompt?
[英]Download .txt using JavaScript without dialog prompt
是否可以僅使用 JavaScript(無服務器端編程!)創建和下載 .txt 文件,並將其保存在本地驅動器上,而不顯示瀏覽器“保存文件”對話框?
Rickard Staaf的回答已經過時。 要在本地下載 javascript 中的文件而不提示對話框,請確保在瀏覽器設置中啟用它(chrome >> 設置 >> 高級 >> 下載並關閉“下載前詢問每個文件的保存位置” 。
隨后,您可以使用blob
對象編寫一個簡單的文本文件:
function save() {
var content = ["your-content-here"];
var bl = new Blob(content, {type: "text/plain"});
var a = document.createElement("a");
a.href = URL.createObjectURL(bl);
a.download = "your-download-name-here.txt";
a.hidden = true;
document.body.appendChild(a);
a.click();
}
不,沒有瀏覽器插件,這將是一個很大的安全風險。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.