简体   繁体   English

在没有对话框提示的情况下使用 JavaScript 下载 .txt

[英]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.

相关问题 如何在没有“离开页面”对话框或任何其他提示的情况下下载文件? - How to download a file without the “Leave the Page” dialog or any other prompt? Excel 在没有对话框提示的情况下在 Inte.net Explorer 中下载 - Excel download in Internet Explorer without Dialog Box Prompt 是否可以使用javascript在特定目录中保存诸如.swf,.mp3,.txt等文件,而无需“另存为”提示? - Is it possible to save a file such as .swf, .mp3, .txt, etc. without the “save as” prompt using javascript in a specific directory? 浏览器使用JavaScript下载文件提示 - Browser download file prompt using JavaScript 使用纯 javascript 创建下载提示 - Creating download prompt using purely javascript 使用javascript用户名/密码提示下载 - Download using javascript username/password prompt 将 blob 转换为 .doc、.docx、.xls 或 .txt 以在浏览器中查看,无需使用 Javascript 下载 - To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript Double Javascript对话框提示 - Double Javascript Dialog prompt 在浏览器中下载JavaScript提示 - JavaScript Prompt download in browser 使用javascript第二次没有出现下载对话框 - Download dialog not appearing for the second time using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM