保存巨大的文件

时间:2016-03-30 18:37:20

标签: javascript html5-filesystem web-mediarecorder

我需要在JS中保存一个未知大小的文件,可能是几千兆字节。数据源是使用mediarecorder捕获的媒体流。

在Chrome中,这可以通过文件系统和文件写入器apis与filesystem:url来完成,方法是在收到文件时将blob块写入文件条目,然后设置文件URL的下载链接。

但是,我无法在Firefox或Edge中找到这样做的方法(无论何时获得mediarecorder)。

1 个答案:

答案 0 :(得分:2)

这适用于我的Firefox:

while(p1!=NULL && p2!=NULL && p1->val!=p2->val) {
   ....
navigator.mediaDevices.getUserMedia({ video: true })
  .then(stream => record(stream, 5000)
    .then(recording => {
      stop(stream);
      video.src = link.href = URL.createObjectURL(new Blob(recording));
      link.download = "recording.webm";
      link.innerHTML = "Download blob";
      log("Playing "+ recording[0].type +" recording.");
    })
    .catch(log).then(() => stop(stream)))
  .catch(log);

var record = (stream, ms) => {
  var rec = new MediaRecorder(stream), data = [];
  rec.ondataavailable = e => data.push(e.data);
  rec.start();
  log(rec.state + " for "+ (ms / 1000) +" seconds...");
  var stopped = new Promise((r, e) => (rec.onstop = r, rec.onerror = e));
  return Promise.all([stopped, wait(ms).then(() => rec.stop())])
    .then(() => data);
};

var stop = stream => stream.getTracks().forEach(track => track.stop());
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var log = msg => div.innerHTML += "<br>" + msg;

用户仍需单击下载链接。我还没有尝试过文件的大小。