docs/Share-Files-using-Filejs.html
Relevant tutorial: How to share files using WebRTC Data Channel (RTP/SCTP) APIs?
This tutorial introduces a new library called "File.js" which can be used alongwidth any framework or realtime protocol like WebSockets/SCTP/etc. to share files of any size concurrently.
<script src="https://www.WebRTC-Experiment.com/File.js"></script>
var progressHelper = {};
var outputPanel = document.body;
var fileHelper = {
onBegin: function (file) {
var div = document.createElement('div');
div.title = file.name;
div.innerHTML = '<label>0%</label> <progress></progress>';
outputPanel.insertBefore(div, outputPanel.firstChild);
progressHelper[file.uuid] = {
div: div,
progress: div.querySelector('progress'),
label: div.querySelector('label')
};
progressHelper[file.uuid].progress.max = file.maxChunks;
},
onEnd: function (file) {
progressHelper[file.uuid].div.innerHTML = '<a href="' + file.url + '" target="_blank" download="' + file.name + '"<' + file.name + '</a>';
},
onProgress: function (chunk) {
var helper = progressHelper[chunk.uuid];
helper.progress.value = chunk.currentPosition || chunk.maxChunks || helper.progress.max;
updateLabel(helper.progress, helper.label);
}
};
function updateLabel(progress, label) {
if (progress.position == -1) return;
var position = +progress.position.toFixed(2).split('.')[1] || 100;
label.innerHTML = position + '%';
}
File.Send({
file: file,
channel: peer,
interval: 100,
chunkSize: 1000, // 1000 for RTP; or 16k for SCTP
// chrome's sending limit is 64k; firefox' receiving limit is 16k!
onBegin: fileHelper.onBegin,
onEnd: fileHelper.onEnd,
onProgress: fileHelper.onProgress
});
var fleReceiver = new File.Receiver(fileHelper);
peer.onmessage = function (data) {
fleReceiver.receive(data);
};
On the constructor; you need to pass an object like {onBegin, onEnd, onProgress}:
"onBegin" is fired as quickly as you send a file
"onProgress" is fired on receiving a chunk
"onEnd" is fired on receiving entire file
"receive" method must be called as soon as your signalling solution gets/receives the chunk.
Send MessageEnter your email too; if you want "direct" reply!