简体   繁体   English

Webrtc数据通道:大文件传输期间将数据保存在文件中

[英]Webrtc datachannels: saving data in file during transfer of big files

I'm using the WebRTC data channels to build a file transfer service. 我正在使用WebRTC数据通道来构建文件传输服务。

Its going quite good with smaller files, under 30 Mb or so. 对于小于30 Mb的较小文件,它运行得很好。 Right now on the receiving end I am simply saving the file data in memory, when all data is transferred I save the file. 现在在接收端,我只是将文件数据保存在内存中,当所有数据传输完毕后,我将保存文件。

Kinda like this : 有点像这样:

//On the recieving side
var dataArray = [];
var dcOnMessage= function(event){
    dataArray .push(event.data);
    if(bytesToRecieve == 0)
    {
        var blob = new Blob(dataArray ,{type: incFileDesc.type});
        reader.onload = function (event) {
            saveToDisk(event.target.result,incFileDesc.name);
        }
        reader.readAsDataURL(blob);
    }
}

var saveToDisk = function(fileUrl, fileName) {
        var save = document.createElement('a');
        save.href = fileUrl;
        save.target = '_blank';
        save.download = fileName || fileUrl;
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);

        save.dispatchEvent(event);
        (window.URL || window.webkitURL).revokeObjectURL(save.href);
    }

So I want to save the data on a file on disk, and then write directly to that file. 因此,我想将数据保存在磁盘上的文件中,然后直接写入该文件。 But how do I do that? 但是我该怎么做呢?

Due to the lack of a way to append data to a blob (see the BlobBuilder API which was never implemented in all browsers) what you do is currently the best way to do it. 由于缺乏将数据追加到Blob的方法(请参阅从未在所有浏览器中实现的BlobBuilder API),当前的操作是最好的方法。 That might change once Chrome (like Mozilla already does) supports sending blobs over the datachannel . 一旦Chrome浏览器(如Mozilla已经支持)支持通过datachannel发送Blob,这种情况可能会改变。

The filetransfer sample works reasonably well for files up to a gigabyte. 文件传输示例适用于高达1 GB的文件。

I don't think you can save files on disk (for security reasons), but you can save it to the indexedDB as a BLOB. 我不认为可以出于安全原因将文件保存在磁盘上,但可以将其作为BLOB保存到indexedDB中。 IndexedDB is widely supported now (see http://caniuse.com/#search=indexeddb ) and is suited for local large objects store. 现在,IndexedDB得到了广泛的支持(请参阅http://caniuse.com/#search=indexeddb ),并且适合于本地大型对象存储。 See https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API for more details about the API. 有关API的更多详细信息,请参见https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API Here is an example for saving BLOB in IndexedDB: https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/ 以下是在索引数据库中保存BLOB的示例: https ://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

I'm afraid the current standardized APIs don't easily allow that (see Philipp's response). 恐怕当前的标准化API不会轻易允许这样做(请参阅Philipp的回复)。 The closest would be to save each as a blob/etc in localstorage/indexeddb, then use a Blob constructor to build the final file from the set of blobs. 最接近的是将每个另存为blob / etc,保存在localstorage / indexeddb中,然后使用Blob构造函数从该blob集中构建最终文件。 It will still have a temporary memory hit of roughly 2x filesize. 它将仍然有大约2倍文件大小的临时内存命中。 Or just hold onto each blob in memory until building the final Blob and saving to disk (still a memory hit, but gradual until it goes 2x when building the final blob). 或者只是抓住内存中的每个Blob,直到构建最终的Blob并将其保存到磁盘为止(仍然是内存命中率,但是逐步构建,直到构建最终的Blob达到2倍时为止)。 These likely start having problems when the sizes of the final files get in the magnitude range of the available RAM. 当最终文件的大小在可用RAM的大小范围内时,这些可能开始出现问题。

Direct transfer of a single large Blob in Firefox->Firefox works today without SCTP ndata support (which isn't available yet) using a deprecated low-level chunking mechanism; 如今,在不使用SCTP ndata支持(尚不可用)的情况下,使用已弃用的低级分块机制,可以在Firefox-> Firefox中直接传输单个大Blob; it avoids the 2x part of the memory hit. 它避免了内存不足的2倍。

There's a non-standard API in Chrome that can mostly do the append-to-file part, last I checked. 我上次检查了一下,Chrome中有一个非标准的API,该API通常可以执行附加到文件的部分。 This has been an ongoing area of discussion with the WebAPI folk; 这是WebAPI人士一直在讨论的领域。 it's probably time to poke them again. 现在可能是再次戳他们的时候了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM