0

I never used JavaScript before. So please excuse my question if it's naive.

I am reading the following W3.org page. In there, they say that a binary file should be downloaded as a blob and in Asynchronous mode. I tried to find other documents but I couldn't file a clear explanation.

File or Blob reads should happen asynchronously on the main thread, with an optional synchronous API used within threaded web applications. An asynchronous API for reading files prevents blocking and UI "freezing" on a user agent’s main thread.

Questions

  • Does it mean that a binary file can only be downloaded in Asynchronous mode and as a Blob in Edge and other modern browsers with JavaScript and thus Typescript?
  • Why can't we download them in a text file like a JSON structure. Is there a size limit on the string size that it is transporting?
  • Does that also apply to old browsers? Meaning on IE can we download the binary files as text files, like for instance a JSON data structure.
Zoe
  • 27,060
  • 21
  • 118
  • 148
Hani Gotc
  • 840
  • 11
  • 24
  • 3
    Microsoft Edge is the name of a browser, not code. The phrase "new browsers" is also not code. It's unclear what is meant by "downloaded as `Async`" since there is no way to download synchronously in modern browsers. JavaScript and TypeScript are trademarks and are spelled with a capital S (and capital J and T respectively). JSON is a standard and is an acronym like HTML and CSS (or API) and thus should be all capitals. Internet Explorer is typically abbreviated IE rather than I.E.. I'm writing these out because every time I tried to edit, you made an edit almost immediately. – Heretic Monkey Jul 19 '21 at 20:33
  • sorry @HereticMonkey my bad – Hani Gotc Jul 19 '21 at 20:34
  • No worries. Edit as you see fit; just wanted to let you know what my edits would be if you wanted to make them yourself :). – Heretic Monkey Jul 19 '21 at 20:35
  • @HereticMonkey I.E. used ActiveX yes? But from what I see it was only used till Internet Explorer 11. In ActiveX. the downloading of binary file was possible in a text and in a synchronous mode? Or it has nothing to do with it – Hani Gotc Jul 19 '21 at 20:40
  • 2
    Text, json, xml are only ways to interpret a byte stream. Under the hood the download is always "binary" until you try to give it a meaning. – Wiktor Zychla Jul 19 '21 at 20:48
  • 1
    Nothing to do with the File API. ActiveX was a way for developers to invoke COM objects to access XMLHttpRequest back before IE7 incorporated XMLHttpRequest as a native built-in object. Ancient history, really. – Heretic Monkey Jul 19 '21 at 20:52
  • 2
    All files are binary. Some can be **interpreted** as text. If you believe your blob is text eg. JSON or csv then you can just do `blob.text()`. There is nothing to worry about downloads being blobs - you just haven't told javascript what type of data you've downloaded. – slebetman Jul 19 '21 at 20:55
  • The section you quote from is really about how user agents (i.e., browsers) should implement the File API. It doesn't really have anything to do with how you as a consumer of the API will interact with it. You don't need to worry about threads or blocking or any of that. You consume the File API by using its event-based API (`onload`, `onerror`) and methods (`readAsArrayBuffer`, `readAsDataUrl`, etc.) and let the browser makers worry about the rest :). – Heretic Monkey Jul 19 '21 at 20:59

1 Answers1

1

I assume your question is about pragmatically reading network resource, i.e., XMLHttpRequest and fetch API, not about the file downloading capability of browsers (which is not a programming question).

The File API you have referenced can be used with these two, but can also be used separately to read local files. In fact, it's very rare to use File API with network requests.

Does it mean that a binary file can only be downloaded in Asynchronous mode

Yes. Since Firefox 30, Chrome 39, Edge 13, and other browsers released near that period, all network requests must be asynchronous. The reason is that JavaScript has only one thread, synchronously waiting a file download will completely freeze the webpage.

Browsers before that can use the { async: false } option in XMLHttpRequest.open() API for sending synchronous requests.

Does it mean that a binary file can only be downloaded as a Blob

No. Everything on the Internet is transmitted in binary format, if you think your data can be interpreted as text, browsers also provide APIs to easily convert between representations (text, JSON, etc.).

Like many other programming languages, you can also write your own logic (for example, protobuf.js) to parse the binary data into the representation you want.

Does that also apply to old browsers? Meaning on IE can we download the binary files as text files, like for instance a JSON data structure.

Actually, before HTML5 ArrayBuffer/Blob/File APIs are added, you can only get response in text format. However, it's still possible to convert the text back into binary.

Why can't we download them in a text file like a JSON structure.

As mentioned above, you can.

Is there a size limit on the string size that it is transporting?

The only limitation is device memory or storage.

yume_chan
  • 858
  • 9
  • 22