12

Is there a way to handle very large files(like 2GB and over) locally in JavaScript without crashing the browser?

*I am aware of the input tag and the FileReader API, but it seems there is nothing like Node.js streams.

8
  • 1
    What exactly are you planning on doing with this file? Just curious.
    – SiddAjmera
    Nov 10, 2018 at 10:04
  • This is mainly hypothetical for future development at the moment, but the reason I thought of asking this question was because of wanting to extract data from a dump of my Google data. Obviously, any kind of functionality I would use this for is to extract a smaller amount of data at once such as "find all dates when I am at specific location". Nov 10, 2018 at 10:07
  • Any reason why you would be asking js to strain under a job that serverside would find trivial?
    – lucas
    Nov 10, 2018 at 10:17
  • @lucas No network upload so reduces data costs + can be done on slow network environments. Users can share the processing burden instead of having the server owner burden the load of everyone using the service. Don't like uploading private data to external server. Nov 10, 2018 at 10:28
  • You can offload your long running task (large file handling in this case) to Web Workers (developer.mozilla.org/en-US/docs/Web/API/Worker). The web worker runs within another thread (context) therefore it won't block your main thread (browser)
    – hoangfin
    Nov 10, 2018 at 11:48

1 Answer 1

14

FileReader enables you to read contents of files asynchronously. With respect to large file (2GB in your case), you can use function/method FileReader.readAsArrayBuffer() to read a certain chunk size of a file in the memory hence this won't crash your browser, this blog is a good example.

2
  • 6
    The important part here is the slice() method of Blob. readAsArrayBuffer will just crash the browser the same if you pass a too big Blob to it.
    – Kaiido
    Nov 11, 2018 at 11:00
  • Few years later, but interestingly, in the current version of chromium, if the slice is too big, the "onload" event is never fired. Instead, the "onerror" event says: "NotReadableError".
    – glihm
    Mar 23 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.