是否有可能只使用JavaScript将数据写入文件? [英] Is it possible to write data to file using only JavaScript?

查看:78
本文介绍了是否有可能只使用JavaScript将数据写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript将数据写入现有文件。
我不想在控制台上打印它。
我想实际上将数据写入 abc.txt
我阅读了很多回答的问题,但每个问题都在控制台上打印。
在某些地方给出了代码,但它不起作用。
所以,请任何人可以帮助我如何实际写入数据到文件。



我引用了代码,但它不起作用:
给出错误:

 未捕获的TypeError:非法的构造函数

on chrome和

  SecurityError:操作不安全。 

on Mozilla

  var f =sometextfile.txt; 
$ b writeTextFile(f,Spoon)
writeTextFile(f,Cheese monkey)
writeTextFile(f,洋葱)

函数writeTextFile(afilename,输出)
{
var txtFile = new File(afilename);
txtFile.writeln(输出);
txtFile.close();





$ b

那么我们真的可以使用Javascript还是NOT写数据到文件?
请帮助我
在此先感谢

解决方案

对此的一些建议 -


  1. 如果您尝试在客户机上编写文件,则无法以任何跨浏览器的方式执行此操作。 IE确实有方法来启用受信任的应用程序来使用ActiveX对象来读/写文件。

  2. 如果您试图将它保存在服务器上,那么只需将文本数据传递给服务器,并使用某种服务器端语言执行文件编写代码。

  3. 要在客户端存储相当小的信息,您可以使用cookies。

  4. 使用本地存储的HTML5 API。


I want to Write Data to existing file using JavaScript. I don't want to print it on console. I want to Actually Write data to abc.txt. I Read many answered question but every where they are printing on console. at some place they have given code but its not working. So please can any one help me How to actually write data to File.

I referred the code but its not working: its giving error:

 Uncaught TypeError: Illegal constructor 

on chrome and

 SecurityError: The operation is insecure.

on Mozilla

var f = "sometextfile.txt";

writeTextFile(f, "Spoon")
writeTextFile(f, "Cheese monkey")
writeTextFile(f, "Onion")

function writeTextFile(afilename, output)
{
  var txtFile =new File(afilename);
  txtFile.writeln(output);
  txtFile.close();
}

So can we actually write data to file using only Javascript or NOT? Please Help Me Thanks in advance

解决方案

Some suggestions for this -

  1. If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
  2. If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
  3. To store some information on the client side that is considerably small, you can go for cookies.
  4. Using the HTML5 API for Local Storage.

这篇关于是否有可能只使用JavaScript将数据写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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