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?
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.
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.
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.
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 .
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With