保存从xmpp Strophe si-filetransfer收到的文件 [英] Save received files from xmpp Strophe si-filetransfer

查看:113
本文介绍了保存从xmpp Strophe si-filetransfer收到的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用strophe.si-filetransfer.js插件在我的Web应用程序中实现文件传输。我能够在iq节中接收文件详细信息。我的问题是,如何从iq节中提取文件数据并下载?

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?

我提到的链接解决方法: https://github.com/strophe/strophejs-plugins/tree/master/ibb

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

谢谢

推荐答案

在接收方,您需要收集 fileHandler 功能,然后抓取 ibbHandler 上的所有数据块(例如使用数组),最后你必须加入所有文件部分(数据块)并恢复原始文件。
在下面的示例中(根据您建议的链接改编)我假设文件是​​使用FileReader API和 readAsDataURL()方法,因此数据 base64 编码。

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

这篇关于保存从xmpp Strophe si-filetransfer收到的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆