在Firefox上使用Blob插件 [英] Use Blob on firefox add-on

查看:791
本文介绍了在Firefox上使用Blob插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让下面的代码在firefox插件中工作:

  var oMyForm = new FormData(); 

oMyForm.append(username,Groucho);
oMyForm.append(accountnum,123456); //数字123456被立即转换为字符串123456

// HTML文件输入用户的选择...
oMyForm.append(userfile,fileInputElement.files [0]);

// JavaScript类文件对象...
var oFileBody ='< a id =a>< b id =b>嘿! / b个;< / A>'; //新文件的主体...
var oBlob = new Blob([oFileBody],{type:text / xml});

oMyForm.append(webmasterfile,oBlob);

var oReq = new XMLHttpRequest();
oReq.open(POST,http://foo.com/submitform.php);
oReq.send(oMyForm);

from https://developer.mozilla.org/zh-CN/docs/Web/Guide/Using_FormData_Objects ?redirectlocale = en-US& redirectslug = Web%2FAPI%2FFormData%2FUsing_FormData_Objects

所以我知道我必须使用XPCOM,但是我找不到相当于。我发现这到目前为止:

  var oMyForm = Cc [@ mozilla.org/files/formdata;1\"].createInstance (Ci.nsIDOMFormData); 

oMyForm.append(username,Groucho);
oMyForm.append(accountnum,123456); // 123456立即转换为字符串123456

// JavaScript文件类对象...
var oFileBody ='< a id =a>< b id =b> hey!< / b>< / a>'; //新文件的主体...
var oBlob = Cc [@ mozilla.org/files/file;1\"].createInstance(Ci.nsIDOMFile,[oFileBody],{type:text / XML});

oMyForm.append(webmasterfile,oBlob);

var oReq = Cc [@ mozilla.org/xmlextras/xmlhttprequest;1\"].createInstance(Ci.nsIXMLHttpRequest);
oReq.open(POST,http:// localhost:3000);
oReq.send(oMyForm);

基本上问题是 var oBlob = Cc [@ mozilla.org/这个文件是一个文件/文件; 1]。createInstance(Ci.nsIDOMFile,[oFileBody],{type:text / xml}); 因为@ mozilla.org/files / file; 1 Ci.nsIDOMFile 不正确。请注意,nsIDOMFile是继承自nsIDOMBlob。



任何人都知道该怎么办?



让我们稍微回答一下:
$ b

  • JS代码模块实际上有 Blob File ,而SDK模块不会:(
  • Cu.import ()将返回一个代码模块的全局全局,包括 Blob

  • 我们可以通过导入一个已知的模块,比如 Services.jsm
  • $ b来得到一个有效的 Blob

    完整的测试示例,基于您的代码:

      const {Cc,Ci,Cu} = require(chrome); 
    //这是作弊;)
    const {Blob,File} = Cu.import(resource:// gre /modules/Services.jsm,{});

    var oMyForm = Cc [@ mozilla.org/files/formdata;1\"].createInstance(Ci.nsIDOMFormData);

    oMyForm.append(username,Groucho);
    oMyForm.append(accountnum,123456); // 123456立即转换为字符串123456

    // JavaScript文件类对象...
    var oFileBody ='< a id =a>< b id =b> hey!< / b>< / a>'; //新文件的主体...
    var oBlob = Blob([oFileBody],{type:text / xml});

    oMyForm.append(webmasterfile,oBlob,myfile.html);

    var oReq = Cc [@ mozilla.org/xmlextras/xmlhttprequest;1\"].createInstance(Ci.nsIXMLHttpRequest);
    oReq.open(POST,http://example.org/);
    oReq.send(oMyForm);


    Been trying to get the following code to work in firefox add-on:

    var oMyForm = new FormData();
    
    oMyForm.append("username", "Groucho");
    oMyForm.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"
    
    // HTML file input user's choice...
    oMyForm.append("userfile", fileInputElement.files[0]);
    
    // JavaScript file-like object...
    var oFileBody = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
    var oBlob = new Blob([oFileBody], { type: "text/xml"});
    
    oMyForm.append("webmasterfile", oBlob);
    
    var oReq = new XMLHttpRequest();
    oReq.open("POST", "http://foo.com/submitform.php");
    oReq.send(oMyForm);
    

    from https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects?redirectlocale=en-US&redirectslug=Web%2FAPI%2FFormData%2FUsing_FormData_Objects

    So I know I have to use XPCOM, but I can't find the equivalent. I found this so far:

    var oMyForm = Cc["@mozilla.org/files/formdata;1"].createInstance(Ci.nsIDOMFormData);
    
    oMyForm.append("username", "Groucho");
    oMyForm.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"
    
    // JavaScript file-like object...
    var oFileBody = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
    var oBlob = Cc["@mozilla.org/files/file;1"].createInstance(Ci.nsIDOMFile, [oFileBody], { type: "text/xml"});
    
    oMyForm.append("webmasterfile", oBlob);
    
    var oReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
    oReq.open("POST", "http://localhost:3000");
    oReq.send(oMyForm);
    

    Essentially the problem is var oBlob = Cc["@mozilla.org/files/file;1"].createInstance(Ci.nsIDOMFile, [oFileBody], { type: "text/xml"}); because "@mozilla.org/files/file;1" or Ci.nsIDOMFile is incorrect. Note that nsIDOMFile is inherits from nsIDOMBlob.

    Anyone know what to do?

    Thanks a bunch.

    解决方案

    Let's cheat a little to answer this:

    • JS Code Modules actually have Blob and File, while SDK modules do not :(
    • Cu.import() will return the full global of a code module, incl. Blob.
    • Knowing that, we can just get a valid Blob by importing a known module, such as Services.jsm

    Complete, tested example, based on your code:

    const {Cc, Ci, Cu} = require("chrome");
    // This is the cheat ;)
    const {Blob, File} = Cu.import("resource://gre/modules/Services.jsm", {});
    
    var oMyForm = Cc["@mozilla.org/files/formdata;1"].createInstance(Ci.nsIDOMFormData);
    
    oMyForm.append("username", "Groucho");
    oMyForm.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"
    
    // JavaScript file-like object...
    var oFileBody = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
    var oBlob = Blob([oFileBody], { type: "text/xml"});
    
    oMyForm.append("webmasterfile", oBlob, "myfile.html");
    
    var oReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
    oReq.open("POST", "http://example.org/");
    oReq.send(oMyForm);
    

    这篇关于在Firefox上使用Blob插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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