Skip to content Skip to sidebar Skip to footer

Use Blob On Firefox Add-on

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); // numb

Solution 1:

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);

Post a Comment for "Use Blob On Firefox Add-on"