1

Situation

I am trying to build something to download a file from several mirrors at the same time. The mirrors support resumable download (thank to http partial content and so on...) so I am able to download all the pieces from different location and when everything is here to let the user "download" it thanks to the well known window.URL.createObjectURL. I stole most of the code there : Handle file download from ajax post

But with the approach of the linked question there are 2 major drawbacks for my project :

  • The download needs to be completed for the download dialog box to show up.
  • All the data is managed by the browser's cache and memory.

Question

Is there any way to show up the download dialog box before the end of the download (before calling window.URL.createObjectURL) ? and to update the chosen file later as pieces arrive.

I was looking of something like php's fopen/fseek/fwrite for the file update.

I looked at the File API but found nothing about writing data. I also thought using local storage but all the same all the data would have to be downloaded prior to offer the download dialog box.

EDIT

How I see things :

Cinematic

I want to know if there is a way to download a generated content just like a regular download : to let the user choose the download target file and only then complete the download in user space tempfilename... (move the surrounded box after the SaveAs and change it for user space instead browser space)

Tuckbros
  • 417
  • 3
  • 13

2 Answers2

0

It would be a huge security risk to allow the browser to edit local files on the user's file system, therefore it's not a feature.

It is possible to download the file with the same name, then the user chooses whether to overwrite the old one via their OS's file manager.

NodeJS has the feature (in javascript) you're looking for but it is for server only

  • I don't want to let javascript update any file on the user file system. When you download a file, the user grant you the permission to write data in this file. The plan is to let the user give me this file at the very beginning and before downloading everything. and then to write data in it as it is generated. The question is : is there a way to manage the content of the file being written ? – Tuckbros Apr 24 '20 at 22:46
  • I'll do a bit of research hold up – user9779523 Apr 24 '20 at 23:12
0

It sounds like you're trying to make something similar to bittorrent. I would make an empty array and create a XHR (https://zeptojs.com/#$.ajax) for each slot in the array, compile them into a blob (https://github.com/eligrey/FileSaver.js) and spit it out. There's no need to ask for permission since the file is new. I've done this before but i lost the source for it

  • an optional alert can be used because no dialog box ever shows up – user9779523 Apr 24 '20 at 23:30
  • I know how to write an xhr and I know how to save the file on the disk. everything is in the linked question. My question is how to be able to allow the user to chose the file to download before the end of every single xhr. – Tuckbros Apr 25 '20 at 05:07
  • There is no way to access a file on the user disk (out of the browser inner memory : cookie, localstorage, ...) out of a download. An alert would solve nothing. – Tuckbros Apr 25 '20 at 05:14
  • `var filename = prompt("please enter a filename");` – user9779523 Apr 25 '20 at 14:41
  • The question is "how to update a file ?", even if the user would give the name of an existing file with the prompt, I would not be able to access this file. – Tuckbros Apr 25 '20 at 15:04
  • I guess it depends on what type of files you would be downloading. If you (the programmer) know what the browser is expecting to receive, you could hash the blob (e.g. CryptoJS's SHA256) every time a new chunk is received to see if it matches the expected whole file. If the chunks are coming from multiple places then have their hashes be hashed together then check the final hash value. – user9779523 Apr 25 '20 at 16:49
  • Also if you expect the user to input a file, there is the file input element available. It doesn't need to be sent immidiately when changed. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file – user9779523 Apr 25 '20 at 16:50
  • FileSaver.js will auto-output to the desired filename, but i believe it will not overwrite, just creates a file with a similar name e.g. `file-name.txt (1)` – user9779523 Apr 25 '20 at 17:30