Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file stream using JavaScript in web browser

In web browser, I want to compute sha1 checksum of a huge file in the local filesystem without sending it to a server.

File API supports to read files from local disk but I guess it reads the entire of the file and put all of them into the memory. It may occur a problem if the file is larger than system memory.

Streams API seems to be useful to solve this problem but I couldn't find how to read a file using the API.

Is there any way to read file stream from local disk using javascript in web browser?

like image 733
npcode Avatar asked Sep 03 '13 07:09

npcode


People also ask

How do I open a stream file in JavaScript?

To open a file with JavaScript, use a <input type="file" /> . If you want, you can use JavaScript to display the open dialog by calling the click() method on the file input (the DOM method, not the jQuery method). In the onchange handler, submit the form, read the uploaded file, and write the contents to your page.

What is a file stream in JavaScript?

Streams are one of the fundamental concepts that power Node. js applications. They are data-handling method and are used to read or write input into output sequentially. Streams are a way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient way.

Can you read a 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.

How do you read data from a readable stream?

To retrieve data from a JavaScript ReadableStream object, we need to call a conversion method to convert the ReadableStream to the actual data we can use. to make a GET request to https://httpbin.org/ip with fetch . fetch returns a promise that resolves to a ReadableStream object which we assigned to response .


1 Answers

The file api provides a slice method, so you should be able to read chunks of data

var blob = file.slice(startingByte, endindByte);

The Sha1 class in google's crypto api provides a update method, you should be able to feed the update method with your chunks

source:

  • http://www.html5rocks.com/en/tutorials/file/dndfiles/
  • https://google.github.io/closure-library/api/goog.crypt.Sha1.html
like image 181
flotto Avatar answered Oct 12 '22 10:10

flotto