Save received files from xmpp Strophe si-filetransfer

倾然丶 夕夏残阳落幕 提交于 2019-12-20 02:32:50

问题


I am implementing file transfer in my web application using strophe.si-filetransfer.js plugin. I am able to receive file details within an iq stanza. My question is, How can I extract file data from an iq stanza and download it?

The link I referred to work around: https://github.com/strophe/strophejs-plugins/tree/master/ibb

Thanks


回答1:


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.")
  }
};


来源:https://stackoverflow.com/questions/32526701/save-received-files-from-xmpp-strophe-si-filetransfer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!