JavaScriptFirefox write to text file on local drive example - Stack Overflow

I understand javascript is not designed for file manipulation due to security concerns. However, recent

I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?

I found the following two potential solutions:

  1. How to enable local javascript to read/write files on my PC?

However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?

Thanks

Edit: I tried to use 1. to write to a file, but it work here is what I wrote:

  1. WritingJS.html:
  2. selfcontainedmap2.js:

My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.

I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?

I found the following two potential solutions:

  1. How to enable local javascript to read/write files on my PC?
  2. https://developer.mozilla/en/Code_snippets/File_I%2F%2FO

However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?

Thanks

Edit: I tried to use 1. to write to a file, but it work here is what I wrote:

  1. WritingJS.html: http://pastebin./ZSztcgNx
  2. selfcontainedmap2.js: http://pastie/3391242

My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.

Share Improve this question edited May 23, 2017 at 10:25 CommunityBot 11 silver badge asked Feb 15, 2012 at 22:29 JavaNoobJavaNoob 471 gold badge2 silver badges7 bronze badges 3
  • The accepted answer in your first link not only provides all the code, but an example of reading from a file (right below the first code). Writing is the same thing, except you pass an extra parameter that is the text you want written. – Ken White Commented Feb 15, 2012 at 22:42
  • Hi, thanks for replying. I tried to use it but could not get it to work. I have edited the original question to include what I wrote. Could you spot any mistakes I made? Thank you. – JavaNoob Commented Feb 16, 2012 at 0:35
  • This solution here shows you how to use HTML5 to save files but when wanting to save to local drive you need user input: stackoverflow./a/13779352/1828637 – Noitidart Commented Feb 9, 2015 at 8:46
Add a ment  | 

2 Answers 2

Reset to default 2

You won't be able to access the XPCOM ponents from non-privileged code.

What that means in English is that those APIs are only available to things like addons running on the browser, not on website code.

There is no way (and will never be a way for security reasons) for a website to read/write local files, apart from cookies, without some plugin like flash providing a way of doing it.

function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerTex;
s.WriteLine(text);
s.Close();
}

You can write text to a file and save it in the local drive. A very basic example is as follows,

The TextArea1 is the id of a form element which contains the target text. The function can be called in a button click event.

Hope that helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744939318a4602224.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信