20

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
npcode
  • 1,370
  • 1
  • 14
  • 29

1 Answers1

13

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:

Mat Gessel
  • 562
  • 8
  • 17
flotto
  • 535
  • 5
  • 17
  • 1
    Thanks a lot! It works very well and I write an example here: https://gist.github.com/npcode/11282867 – npcode Apr 25 '14 at 09:04
  • @npcode Thanks for the link - but the link is now dead? – BenKoshy Aug 28 '20 at 08:15
  • 2
    @BKSpurgeon Yeah, you're right. Here is the new link: https://gist.github.com/eungjun-yi/11282867 – npcode Aug 28 '20 at 17:35
  • 1
    Great that it works, but you chunk the file manually with a loop. Isn't the whole point of Streams that you don't have to do this annoying chunking yourself? – Phil Dec 02 '22 at 01:25
  • @Phil You're right, it would be better to you FileReader.onload, it already reads chunks. – flotto Jun 19 '23 at 07:06