Save received files from xmpp Strophe si-filetransfer

半世苍凉 提交于 2019-12-01 21:54:25

On the receiver side, you need to collect file info on fileHandler function, then grab all data chuncks on ibbHandler (e.g. using an array) and finally you have to join all file parts (data chuncks) and restore the original file. In the example below (adapted from the link you suggest) I assume the file is chuncked using the FileReader API and readAsDataURL() method, so data are base64 encoded.

var aFileParts, filename, mimeFile;

var fileHandler = function(from, sid, filename, size, mime) {
    // received a stream initiation
    filename = filename;
    mimeFile = mime;
};
connection.si_filetransfer.addFileHandler(fileHandler);

var ibbHandler = function (type, from, sid, data, seq) {
    switch(type) {
    case "open":
      // new file, only metadata
      aFileParts = [];
      break;
    case "data":
      // data
      aFileParts.push(data);
      break;
    case "close":
      // and we're done
      var data = "data:"+mimeFile+";base64,";
      for (var i = 0; i < aFileParts.length; i++) { 
         data += aFileParts[i].split(",")[1];
      }
      var span = document.createElement('span');
      span.innerHTML = '<a href="'+data+'" download="'+filename+'">'+filename+</a>;
    default:
      throw new Error("shouldn't be here.")
  }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!