Read a local file using javascript - javascript

How do I get the contents of a local file, say:
/home/user/Wired/uploads/1.csv
in a variable x in javascript?
Can I use the filereader http://www.w3.org/TR/file-upload/#dfn-filereader?
If yes, then how?

You can't do this with strict javascript, but you can use the web server as an intermediate between the user and the browser. Have the user upload the file asynchronously (using AJAX). The web server could then return the plain-text value back to the AJAX call.
The contents would then be free to use as you see fit.
This is likely your only option without employing Flash/Silverlight/Java.

Reading client files in javascript is possible indeed with the new File API available in modern browsers (I dont know if IE supports this now). Check this site and its code: http://www.readfileonline.com/ it allows you to read files using the browser only.

Related

How to download files using IE9 with HTTP protocol

I would like to do a file (of any kind) download using IE9 without any redirection. By redirection I mean providing a URL of the resource I am trying to download to the current document forcing a download if the MIME type is not a document type.
So I am left with getting the data using the XHR object and find a way to save it on disk. Since I am using IE9, I can't use any File API provided in IE10+.
So forget about:
using Blob
using FileSaver (https://github.com/eligrey/FileSaver.js/)
using Blob and typedarray polyfills needs debugging and I can't make them work
Right now I am getting the data after the REST call and trying to write it into a document like in this post: Javascript Save CSV file in IE 8/IE 9 without using window.open()
But, the problem is document.write() seems to encode anything written to it in UCS-2, so binaries sent from the back-end are reinterpreted and the file gets corrupted. I am guessing that only text-based files could be saved then.
Last and not the least, I SHOULD not use flash.
Does anyone have an idea in mind to resolve the encoding issue or another technique to do the download?
If it can help, I am using angularjs as a front-end JS framework
For the limitation we had, passing the token as a parameter to get the file downloadable resource was the solution. When we uplift IE, we shall change the solution tough.
It implies removing any security chain filter and do a manual validation on the token in the backend.

file access from javascript code

I'm trying to access a text file using javascript code, but I keep receiving a security error message. What I'm trying to do is:
var file = File('path/filename.txt');
The path is relative to the script location. I found that running from local machine could be the reason for that, so I tried to run from my local webserver but the error is still there. Does someone know why? What can I do to load this text file? If possible, considering that it is always under the script path, could it be also loaded from local disk (no web server)?
Thank you.
If you're trying to access a file on the server, you should do it using a server side coding language like PhP.
If you're trying to access a client file (= a file on the computer of your website visitor), it's not possible for obvious security reasons [EDIT] Thanks to Colin DeClue , I discovered this is possible using the HTML5 File API. An article explaining this is available here : http://www.html5rocks.com/en/tutorials/file/dndfiles/
Your snippet will attempt to load the file from your local file system, so can't retrieve a file from a server. You'd need to look at requestFileSystem or FileReader to make something like this work if the file is local.
Alternatively, you could use AJAX to retrieve the file from a server.

Can I pass a file into the browser then manipulate it with javascript?

I understand that javascript running in the browser would not be allowed to access a local file system directly, it make sense and I am not looking for a way to violate that.
However are there any safe and kosher ways to grab files from the local system for the browser to use? Would these methods be platform specific? Would I be limited it what kinds of files and would access have to be granted on a per file basis?
What I am thinking about is when you upload photos, it will open a system level dialog and then eventually present that file to the browser for the browser to send to a remote server via http. Would it then be possible to pass a file in to the browers local memory/ give the browser permission to read it as it were to be upload but instead of uploading it somehow pass over to java script and allow it to manipulate it client side much in the way you can manipulate files with server side javascript?
The reason I am asking is because I have recently become interested in browser-based file manipulation programs, their feasibility and what could be done with them in a Chrome OS type environment. Just out of curiosity then any idea of it being practical any time soon.
edit: attempted to clarify and make my question more direct.
Use the File API (MDN has a tutorial) but be aware that it is a new specification and has limited browser support.
It allows files selected in an <input type="file"> to be accessed with client side JS.

how to read a text file using Javascript

I am taking a text file from user and then posting that file back to the browser using ajax storing the content in db and then showing the content back to user page using Jquery post response.
Now i want to something like this..
Read the text file from the user computer using javascript. Display the content and when he submits the page I will save the values.
Can't be done in pure JS for security reasons. You would need to have the user upload the file to your server, and fetch the contents back through Ajax.
If you use Flash or Java, you should be able to gain direct access to the file. If you speak Flash/Actionsript, maybe SWFUpload's source code (especially the new client-side resizing functions) can serve as an inspiration.
Update: This blog entry should help. Read and write local files with Flash Player 10
Update: To elaborate on the "upload and fetch" thing, if you do the uploading in an IFRAME, you could even have the upload script simply output the text file's contents. Because the iframe belongs to your domain, you will be able to retrieve its contents via JavaScript when the upload has finished. As long as you send a content-type: application/text it should be fairly safe from any malicious attacks.
If you're ok with Firefox 3.6 support only check out https://developer.mozilla.org/en/Using_files_from_web_applications, otherwise you should use Flash, Java or silverlight for this.
You won't be able to read a file in user's computer due to security issues.
Reading client files in javascript is possible with the new File API available in modern browsers. Check this site and its code: http://www.readfileonline.com/
However, before reading file contents in javascript, the user must explicitly select the files it allows to read. This is a security feature of the standard.

JavaScript to Flash and Back?

I am completely new to flash. We need to load a binary file from the user system (must work in Flash 9) and do some process in flash and save it back to the file system. I think that this would have to be done by getting the file in JavaScript and then encoding it send it to Flash.
Local file system access is in Flash 10 with the File Reference enhancements. You're out of luck if targetting Flash 9.
Javascript cannot access the local filesystem either.
http://weblogs.macromedia.com/flashjavascript/
http://kb2.adobe.com/cps/156/tn_15683.html
Those are two good links reguarding javascript communications
alternatively, you can make a JavaScript Flash file and make a window SWF, which has full file access. However, this could never be used on the web

Categories