简体   繁体   English

如何使用 JavaScript 创建和保存文本文件

[英]How to create and save a text file with JavaScript

Do you know any cross-browser method to create and save a file with JavaScript on the client-side?您知道在客户端使用 JavaScript 创建和保存文件的任何跨浏览器方法吗?

Considerations:注意事项:

  1. I can't save it on the server because the file is going to be read from a fiscal printer.我无法将其保存在服务器上,因为将从财务打印机读取该文件。
  2. The server can't access the client.服务器无法访问客户端。 This is obvious because we are talking about a web application in the web, so the server can't access a client folder.这很明显,因为我们正在谈论 Web 中的 Web 应用程序,因此服务器无法访问客户端文件夹。
  3. New ActiveXObject("Scripting.FileSystemObject"); New ActiveXObject("Scripting.FileSystemObject"); is ONLY for Internet Explorer and even then not for all versions.仅适用于 Internet Explorer,甚至不适用于所有版本。
  4. We are talking about printing on a fiscal printer, so I can't ask the cashier in the supermarket to download the text file and save it in a folder where the fiscal printer can read it.我们说的是在财政打印机上打印,所以我不能要求超市的收银员下载文本文件并将其保存在财政打印机可以读取的文件夹中。
  5. If not JavaScript, what else can I use in my ASP, .Net 4.0, C# web application?如果不是 JavaScript,我还可以在我的 ASP、.Net 4.0、C# web 应用程序中使用什么?

Basically, you said it, "we are talking about a web application."基本上,你说过,“我们正在谈论一个 Web 应用程序。” Do you know any web applications that save a file to your hard drive (besides cookies) without showing you a download prompt first?您是否知道任何将文件保存到硬盘驱动器(Cookie 除外)的 Web 应用程序,而无需先向您显示下载提示?

Having said that, browsers have started to offer a persistence API that goes beyond cookies.话虽如此,浏览器已经开始提供超越 cookie 的持久性 API (See, for example, this article .) But such a solution wouldn't meet your requirement of being cross-browser. (例如,参见这篇文章。)但是这样的解决方案不能满足您的跨浏览器要求。

Your only options are to use persistance APIs from HTML5 or to create a browser plugin (activex control on IE, NPAPI plugin on others) that can do the file access for you.您唯一的选择是使用 HTML5 中的持久性 API 或创建一个浏览器插件(IE 上的 activex 控件,其他人上的 NPAPI 插件)来为您进行文件访问。 You could use FireBreath to do this, and it would be relatively simple if you know C++.您可以使用FireBreath来执行此操作,如果您了解 C++,它会相对简单。

That said, it is a really dangerous idea;也就是说,这是一个非常危险的想法。 it is difficult to prevent people from using your plugin in other pages, so your plugin has to somehow be smart enough to keep itself from being abused by malicious sites that want to read (or even write) arbitrary data to your hard drive.很难阻止人们在其他页面中使用您的插件,因此您的插件必须以某种方式足够聪明,以防止自己被想要读取(甚至写入)任意数据到您的硬盘驱动器的恶意站点滥用。

There is, after all, a reason why browsers don't natively support this.毕竟,浏览器本身不支持这一点是有原因的。 I'd look at HTML5.我会看看 HTML5。

we came out with a simple windows service nodejs app to be installed in the local machine.我们推出了一个简单的 Windows 服务 nodejs 应用程序,可以安装在本地机器上。 When the web application need to create a file it just sends an API call to this app using localhost and that will write the file for it.当 Web 应用程序需要创建文件时,它只需使用 localhost 向此应用程序发送 API 调用,然后为其编写文件。 Cheers.干杯。

你可以通过客户端 Web 服务来做到这一点,只需在 Web 服务中创建一个函数,在你的硬盘驱动器中创建一个文本文件,然后当你这样做时,将 Web 服务转换为客户端服务 .Net 框架制作一个客户端 java 脚本然后您将能够在客户端使用 Java 脚本调用服务器端函数,之后您的文件将不会发布在服务器中,它将继续保存在您的硬盘驱动器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM