Search code examples
javascriptxmlhttprequestfileapifetch-api

How to unify retrieving file slices, supporting both File API and XMLHttpRequest/Fetch API?


I have a file format that I can parse with JavaScript by reading out slices of it. My data file is very big so I don't want to read the whole file but only slices of it. This is solved by having a header in the beginning of the file that contains an index of the data. I've already written a web page that supports opening local files but now I would like to support remote files too.

The File API and the XMLHttpRequest both support retrieving slices, so I would like to write my parsing code as generic as possible and support both local and remote file access at the same time.

Is there any standard way of how to implement this? Is there any API that wraps both local and remote file access into a single API?

I noted there is a new API for remote file access, the Fetch API. Maybe that is related to this question?


Solution

  • https://streams.spec.whatwg.org/ is probably the eventual abstraction for situations such as these, but integration with browsers is limited, and integration with the File API is not existent.

    You probably need to write your own abstraction for now.