[英]Download .txt using JavaScript without dialog prompt
是否可以仅使用 JavaScript(无服务器端编程!)创建和下载 .txt 文件,并将其保存在本地驱动器上,而不显示浏览器“保存文件”对话框?
Rickard Staaf 's answer is outdated. Rickard Staaf的回答已经过时。 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' .要在本地下载 javascript 中的文件而不提示对话框,请确保在浏览器设置中启用它(chrome >> 设置 >> 高级 >> 下载并关闭“下载前询问每个文件的保存位置” 。
Subsequently, you can write a simple text file like so using blob
objects:随后,您可以使用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.