在没有对话框提示的情况下使用JavaScript下载.txt [英] Download .txt using JavaScript without dialog prompt
问题描述
是否可以仅使用JavaScript创建和下载.txt文件(无需服务器端编程!),然后将其保存在本地驱动器上,而无需显示浏览器保存文件"对话框?
Is it possible to create and download a .txt file using only JavaScript (no server-side programming !), and save it on local drive, without displaying browser "Save file" dialog ?
推荐答案
Rickard Staaf 的答案已过时.要在本地使用javascript下载文件而不提示对话框,请确保在浏览器设置(chrome >>设置>>高级>>下载)中启用该文件,然后关闭在下载前请问保存每个文件的位置" .
Rickard Staaf's answer is outdated. To download a file in javascript locally without prompting a dialog box, be sure to enable it in your browser settings (chrome >> settings >> advanced >> downloads and turn off 'Ask where to save each file before downloading'.
随后,您可以使用 blob
对象像这样编写一个简单的文本文件:
Subsequently, you can write a simple text file like so using blob
objects:
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();
}
这篇关于在没有对话框提示的情况下使用JavaScript下载.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!