Download the file on the client side with a piece

I use WebRTC to send the file to the connected peer and I send the file to chunks. However, I am having trouble figuring out how to get my peer to save / load a file, when it is broadcast, a piece of fragment.

All the examples I found on the Internet recommend doing something like this:

// sender dataConnection.send({ 'file': file }); // receiver dataConnection.on('data', function(fileData) { var dataView = new Uint8Array(fileData); var dataBlob = new Blob([dataView]); var url = window.URL.createObjectURL(dataBlob); // create <a> var link = document.createElement('a'); link.href = url; link.download = fileName; document.body.appendChild(link); // trigger the download file dialog link.click(); } 

This approach, however, does not support receiving fragments of a file and recording each fragment as it arrives ... it must wait until the entire file is read on the sender side and sent to the receiver.

What I'm trying to do is something like this:

 // sender for (var i = 0; i < fileSize; i += chunkSize) { var fileReader = new FileReader(); // read next chunk var blob = file.slice(start, end); ... fileReader.onload = function(e) { ... dataConnection.send({ 'blob': blob }); } fileReader.readAsArrayBuffer(blob); } 

Thus, I read a piece of the file in a piece and send each piece to the receiver when I read it. Now, the only way I know how to actually save the file that is sent this way is to do what is described in this blog post:

http://bloggeek.me/send-file-webrtc-data-api

... is described in "Step 6: Download to regular FS" . However, this approach takes all the pieces as they arrive, stores them in memory, then builds a large UInt8Array in memory, and then allows the receiver to load the file. It is really heavy in memory and really limited, perhaps a couple of hundred MB, so it does not scale.

Is there a way to open the file download dialog after the first piece comes in and keep writing chunks when they come in so that the download is a "streamed" download?

+2
source share
1 answer

UPDATE

Thread API: https://streams.spec.whatwg.org

https://jakearchibald.com/2016/streams-ftw


Unfortunately, from what I investigated, there is no way to open the file / file save dialog and save / load the file using the "streaming" method.

The approach I'll take is to use the file system API . Unfortunately, this is not supported by all browsers:

... and it is unlikely that many browsers will use this API :(

+1
source

Source: https://habr.com/ru/post/989919/


All Articles