Read file stream using javascript in web browser

2019-03-25 04:47发布

问题:

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?

回答1:

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