在浏览器中写入没有外部JS库(纯Javascript)的文件? [英] Write to a file without external JS libraries (pure Javascript) in a browser ?

查看:151
本文介绍了在浏览器中写入没有外部JS库(纯Javascript)的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以写一个字符串到一个文件(如果它不存在,就创建这个文件)
没有任何外部的Javascript库,纯粹用Javascript代码?

我试图实现一个客户端代码,当它到达一个页面(.ASPX)
时,它将几个字符串写入一个文件(比如说 output.txt )就是这样。

通过互联网看到的一切,JavaScript都不允许向硬盘写任何东西。

是这样吗?没有解决方法?

正如评论中所说,您试图达到的目标是非常受限制的,因为访客的机器。

现在解决方法可以存在,这取决于你正在实现的实际目标(但是你没有提到)。


  • 在访问者的计算机中存储简单数据(如字符串)的Cookies(严格来说,它可以完成您要求的工作,但您无法控制哪个文本文件)
  • 使用一个浏览器扩展,它有权限在访问者的文件系统上写入。如果您可以让访问者安装这样的扩展(例如,如果您是IT人员,并且可以请求员工安装特定的软件),则适用。
  • 如果您只是想要一些持久性数据,请参阅IanKenney的评论中提出的
  • localStorage 。


Is it possible to write a string to a file (create the file if it doesn't exist) without any external Javascript libraries , purely with Javascript code ?

I'm trying to implement a client side code that whenever it reaches a page (.ASPX) it writes few strings into a file (let's say output.txt) and that's it .

From everything I see over the internet , JavaScript is not allowed to write anything to the hard drive .

Is that so ? no workarounds ?

解决方案

As said in the comments, what you sound to try to achieve is intentionally very restricted for obvious security reasons of the visitor's machine.

Now "workarounds" could exist, depending on the actual objective you are trying to achieve (but which you did not mention).

  • Cookies to store simple data (like your strings) in the visitor's computer (so strictly speaking, it does the job you ask for, but you have no control on which text file is used).
  • Use a browser extension, which would have the permission to write on the visitor's filesystem. Applicable if you can get visitors to install such extension (e.g. if you are IT and can request employees installing specific software).
  • localStorage as proposed in IanKenney's comment, if you just want some persistent data.

这篇关于在浏览器中写入没有外部JS库(纯Javascript)的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆