Save received files from xmpp Strophe si-filetransfer

前端 未结 1 1870
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 04:04

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 e

相关标签:
1条回答
  • 2021-01-21 04:50

    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.")
      }
    };
    
    0 讨论(0)
提交回复
热议问题