Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load .txt file from javascript with jquery [closed]

I want to load a local .txt file and work with the content in javascript. My local file is like C:\Users\Who\Desktop\file.txt Thanks

like image 709
Luccas Avatar asked Jul 21 '10 22:07

Luccas


People also ask

Can you read a text file in JavaScript?

To read a file, use FileReader , which enables you to read the content of a File object into memory. You can instruct FileReader to read a file as an array buffer, a data URL, or text.

Can JavaScript write a text file?

There is a built-in Module or in-built library in NodeJs which handles all the writing operations called fs (File-System). It is basically a JavaScript program (fs. js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system.

How do I read a text file in HTML?

Make sure you check the source of the document once it's loaded in the browser (all browsers let you do this, right-click "view page source" or similar). If you see the contents of version. txt anywhere in there, you're on the right track, you just need to move it into the body tag so that it will be rendered.

How do I save a text file in JavaScript?

The possible ways to create and save files in Javascript are: Use a library called FileSaver – saveAs(new File(["CONTENT"], "demo. txt", {type: "text/plain;charset=utf-8"})); Create a blob object and offer a “save as”.


1 Answers

by default javascript is NOT allowed to access local file system for security reasons. If you want to allow a particular script access to a local file then you have 2 options.

1a. Change your model, put the text file on a server and load from there...

1b. Run a local webserver :-)

2 ... this becomes browser dependent,

In particular,

  • you can create a signed javascript for Mozilla like browsers, see http://www.mozilla.org/projects/security/components/signed-scripts.html for details

  • you can create an ActiveX plugin that allows local access for IE types... ;

  • and for anything else again read up o local access.

like image 138
Elf King Avatar answered Oct 30 '22 07:10

Elf King