1

I am making a file sharing app in java-script. I have to send and receive large files which I cannot store in buffer or memory. I want to directly store each coming data chunk directly to disk. Any reference link will be appreciated.

  • Are you trying this using browser (frontend) javascript? Because In JavaScript you cannot have the direct access to the filesystem. However, you can make browser to pop up a dialog window allowing the user to pick the save location. This saving is not controlled by javascript and is standard download. More info [here](http://stackoverflow.com/a/7952401/724913) – arkoak Apr 06 '15 at 04:07
  • @arkoak: the problem with that download method is that it needs the file data in a buffer/string. most browsers allow the "download or open" coice to be persisted by MIME, so if you tell, for example, a RTF file to always save, then you can init large downloads of such a file from JS without human intervention or JS-intermediaries. – dandavis Apr 06 '15 at 05:18
  • Yes I'm using front-end java-script. It is a peer to peer file sharing app. – Sadiq Ahmad Apr 06 '15 at 05:32
  • @Sadiq, [this](https://github.com/Miserlou/DirtyShare) should get you started. – arkoak Apr 06 '15 at 05:41

1 Answers1

1

FileWriter is a good place to start, if you only care about Chrome.

Otherwise, an interesting hack is using IndexedDB to store chunks as blobs, because they'll technically be stored to disk, then constructing a big blob out of these chunks, and providing a link to it with URL.createObjectURL. It doesn't involve loading anything into memory, since blobs are just references to data, not the data itself, and in this case, all the data is stored off-memory, inside IndexedDB. The only problem here is the extra copy of all the data.

It's not as nice as FileWriter, but this hack is the only solution to work across many browsers (Safari being a notable exception, as always.)

Shien
  • 495
  • 5
  • 13