13

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. Commented Nov 10, 2018 at 10:04
  • 1
    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". Commented Nov 10, 2018 at 10:07
  • Any reason why you would be asking js to strain under a job that serverside would find trivial? Commented Nov 10, 2018 at 10:17
  • 1
    @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. Commented Nov 10, 2018 at 10:28
  • 1
    @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. Commented Nov 10, 2018 at 13:13

1 Answer 1

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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".

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.