0

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.

2
  • Can you get to the file from the browser? Commented Jun 17, 2013 at 13:52
  • yes, the file is accessible from the browser Commented Jun 17, 2013 at 14:08

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

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. Yes it is, in modern browsers. Read up on HTML5 File API.
Indeed, I had overlooked that possibility. I will update my answer accordingly. Thanks for pointing this out, I will remember it for sure !
0

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.

4 Comments

How could I load it using FileReader? The file to load is chosen from user interaction: in this case I want to load a subtitle text file based on a video source.
This answer seems like it could imply that you could use requestFileSystem or FileReaser to read files from the server. That's not the case. Those just make reading files from the user's local file system easier. If you need to read from the server, you need server side code.
@ColinDeClue Wording tweaked to clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.