简体   繁体   English

是否可以使用 JavaScript 写入文件(在磁盘上)?

[英]Is it possible to write to a file (on a disk) using JavaScript?

I am a novice-intermediate programmer taking a stab at AJAX.我是一名初学中级程序员,正在尝试 AJAX。 While reading up on JavaScript I found it curious that most of the examples I've been drawing on use PHP for such an operation.在阅读 JavaScript 时,我发现我所绘制的大多数示例都使用 PHP 进行此类操作很奇怪。 I know many of you may argue that 'I'm doing it wrong' or 'JavaScript is a client-side language' etc. but the question stands.我知道你们中的许多人可能会争辩说“我做错了”或“JavaScript 是一种客户端语言”等,但问题仍然存在。 . . .can you write a file in only JavaScript? . 你能只用 JavaScript 编写文件吗?

Yes, of course you can.是的,当然可以。 It just depends on what API objects your javascript engine makes available to you.这仅取决于您的 javascript 引擎提供给您的 API 对象。

However, odds are the javascript engine you're thinking about does not provide this capability.但是,您正在考虑的 javascript 引擎很可能不提供此功能。 Definitely none of the major web browsers will allow it.绝对没有主要的网络浏览器会允许它。

You can write cookies with Javascript, on newer browsers you also have an SQLite database to store client side data.您可以使用 Javascript 编写 cookie,在较新的浏览器上,您还有一个 SQLite 数据库来存储客户端数据。 You cannot store data in an arbitrary location on the disk though.但是,您不能将数据存储在磁盘上的任意位置。

You can use something like Google Gears to produce JS applications which are capable of storing data in a local cache or database. 您可以使用 Google Gears 之类的工具来生成能够将数据存储在本地缓存或数据库中的 JS 应用程序。 You can't read or write arbitrary areas of the disk though. 但是,您无法读取或写入磁盘的任意区域。 (This was written in 2009 - Google Gears is now deprecated ) (这是在 2009 年编写的 - Google Gears 现在已弃用

These days, you should be looking at the local storage capabilities provided by HTML5这些天,您应该关注HTML5 提供本地存储功能

不可以。您可以使用 JavaScript 创建对服务器端处理脚本的 AJAX 请求,但允许 JS 直接写入磁盘——无论是客户端还是服务器端——将是一个巨大的、讨厌的、明显的、不可原谅的浏览器安全漏洞.

The short answer is no;简短的回答是否定的; you cannot by default write a file to the local disk, by using plain JavaScript in a browser.默认情况下,您无法通过在浏览器中使用纯 JavaScript 将文件写入本地磁盘。 You'll need a helper to do that.你需要一个帮手来做到这一点。 For example, TiddlyWiki is a wiki engine that is just a single, static HTML file, but it can write itself to disk with the help of a Java applet (Tiddly Saver).例如, TiddlyWiki是一个 wiki 引擎,它只是一个单一的静态 HTML 文件,但它可以在 Java 小程序 (Tiddly Saver) 的帮助下将自身写入磁盘。

您可以在 Windows Scripting Host 中。

Next version of chrome (v52) made this possible with fetch api + service worker + streams, you can enable streams now with a flag...下一版本的 chrome (v52) 通过 fetch api + service worker + 流使这成为可能,您现在可以使用标志启用流...

you can go to the StreamSaver.js to see some examples of how to use it.您可以转到StreamSaver.js以查看有关如何使用它的一些示例。

You can do something like this:你可以这样做:

const writeStream = fs.createWriteStream('filename.txt')
const encoder = new TextEncoder
let data = 'a'.repeat(1024)
let uint8array = encoder.encode(data + "\n\n")

writeStream.write(uint8array)
writeStream.close()

Or just go ahead and look at the demos: https://jimmywarting.github.io/StreamSaver.js/example.html或者继续看演示: https : //jimmywarting.github.io/StreamSaver.js/example.html

Nope, Javascript is not allowed to access the filesystem at all, its a security restriction in the browser.不,根本不允许 Javascript 访问文件系统,这是浏览器中的安全限制。 The only way you can really do it is with ActiveX, but then your limiting yourself to using IE.您真正可以做到的唯一方法是使用 ActiveX,但是您只能使用 IE。

Edit: AS the above post states, it could be possible if your engine allowed it, however I don't know of one browser engine (which is what I asusme you are writing it for) that will allow you to.编辑:正如上面的帖子所述,如果您的引擎允许,则有可能,但是我不知道有一种浏览器引擎(这就是我假设您正在编写它的目的)可以让您这样做。

If you just need to let user download a file ( .txt , .csv , images and others) via browser download dialog , you can use data URIs with <a href=... download=.../> tag.如果您只需要让用户通过浏览器下载对话框下载文件( .txt.csv 、图像等),您可以使用带有<a href=... download=.../>标签的数据 URI

For example (for text file):例如(对于文本文件):

<a href="data:text/plain;charset=utf-8,TEXT_HERE" download="filename.txt"> Click to download </a>

You can also set the attribute href and download using javascript, and use element.click() to trigger the download.您还download使用 javascript 设置属性hrefdownload ,并使用element.click()触发下载。

However, this method cannot write a file without user confirming the file download dialog.但是,如果没有用户确认文件下载对话框,此方法无法写入文件。

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

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