7

Is it possible to read a file in from a path in JavaScript and create a byte[] of that file?

1
  • I think you should give us more clues about what exactly you're trying to do. Commented Aug 24, 2009 at 15:26

5 Answers 5

18

Yes, you can — in Firefox, anyway. Other browsers may or may not choose to allow it in the future.

Make a file upload field for the user to pick the file, and read it through the input.files list. eg. document.getElementById('myuploadfield').files[0].getAsBinary(). This puts each byte in a single character of a JavaScript String, which is about as close to a byte[] as you're going to get.

This is quite a specialized interface and probably Not The Right Thing — heed the other replies, because it's very possible you are trying to do something in a inappropriate way. Difficult to tell without context.

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

Comments

7

There are two forms of JavaScript: client-side and server-side. In client-side JavaScript it is not possible, while in server-side JavaScript, it is possible. So it depends on whether you are using client-side or server-side JavaScript.

That said, client-side JavaScript is much more common and if you were using server-side JavaScript, you would probably know the answer to your question. So I'm going to go out on a limb and say that no, it is not possible to read in from a path in JavaScript and create a byte[] from that file.

(Also, it's unclear what you mean by a byte[]; that's not a common notation when using JavaScript. Are you sure you aren't talking about Java? Java is completely different from JavaScript: in Java byte[] is a more common notation.)

2 Comments

I believe he just wanted to know if he could load the file's bytes into an array. Javascript doesn't have this notation, but an array of numbers would essentially do the same thing. Do you have any links about serverside javascript? I haven't used server side javascript and am I curious as to where it is used.
how do you do this in server-side javascript? It is much more common now in 2015 but all the answers about this kind of question on this website are about client-side js
5

No. JavaScript is purposely designed to have very minimal file IO (think cookies) because allowing it to access arbitrary files (local and remote) would be a massive security risk.

1 Comment

For all who write about file-i/o and security risk: For web originated JavaScript executed in the browser file i/o is a security risk. Does not depend on the programming language, though. The risk, as stated here, is basically applicable to all programming languages.
3

You guys seem to have completely forgotten mobile platforms and javascript designed to run with no-security flags set what-so-ever. I access my files just fine in my javascript code, which runs on my iPhone. You just cant access the users filesystem, only your own filesystem which is basically a folder isolated from the rest of the system.

Sp his question was perfectly valid. Also - you should look up Javascript blobs (binary large objects) and typed arrays. You can allocate "normal" byte, word and longword arrays in JS - but people rarely use them.

Comments

0

File I/O in Javascript is considered a serious security risk:

http://forums.devshed.com/javascript-development-115/file-i-o-with-javascript-10376.html

Comments

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.