Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript file to byte[]

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

like image 583
Jack Avatar asked Aug 24 '09 12:08

Jack


People also ask

How to Read file as byte array in JavaScript?

var bytes = []; var reader = new FileReader(); reader. onload = function () { bytes = reader. result; }; reader. readAsArrayBuffer(myFile);

What is byte in Javascript?

The byte() function in p5. js is used to convert the given string of number, number value or boolean into its byte representation. This byte number can only be whole number in between -128 to 127.

What is a Bytearray?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..


1 Answers

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.

like image 163
bobince Avatar answered Oct 24 '22 12:10

bobince