11

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.

Damien Golding
  • 1,003
  • 4
  • 15
  • 28
  • 1
    What exactly are you planning on doing with this file? Just curious. – SiddAjmera Nov 10 '18 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". – Damien Golding Nov 10 '18 at 10:07
  • Any reason why you would be asking js to strain under a job that serverside would find trivial? – lucas Nov 10 '18 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. – Damien Golding Nov 10 '18 at 10:28
  • You can offload your long running task (large file handling in this case) to Web Workers (https://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 '18 at 11:48
  • @meteorzeroo Nice tip, but does not solve my issue as "crash" is not referring to a pause in processing due to CPU usage but instead that the max internal memory used by the browser process has been exceeded. – Damien Golding Nov 10 '18 at 13:13
  • FileReader.readAsArrayBuffer() is your friend for this situation. This [blog](http://joji.me/en-us/blog/processing-huge-files-using-filereader-readasarraybuffer-in-web-browser) is a good reference for your case – hoangfin Nov 10 '18 at 14:34
  • @meteorzeroo Thanks. You should add that as the answer. – Damien Golding Nov 11 '18 at 04:28

1 Answers1

13

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.

hoangfin
  • 667
  • 7
  • 16
  • 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 '18 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 '23 at 04:53