Search code examples
firefox-addonfirefox-addon-sdk

How to make Firefox handle the result of a POST request with firefox-addon-sdk?


I'd like to post images from my addon to my Servlet that processes the images and returns a PDF. The servlet works. I just don't know how to handle the resulting PDF from my addon.

const request= require("sdk/request").Request;
...
            let req= request({
                url: "http://localhost:8090/Whatever/PdfServlet",
                content: params,
                onComplete: function (response) {
                    console.log(response.text)
                }
            });
            req.post();

Here, object params contains the base64 encoded images. Everything works, I can see the beginning of the PDF stream in the console log. But how do I make Firefox show its open/save dialog so that the user can save or view the PDF?


Solution

  • Here's a solution:

          const querystring= require('sdk/querystring');
          const winUtils= require('sdk/window/utils');
    
    ...
                let stringStream= Cc["@mozilla.org/io/string-input-stream;1"].
                       createInstance(Ci.nsIStringInputStream);
                stringStream.data= querystring.stringify(params);
                let postData= Cc["@mozilla.org/network/mime-input-stream;1"].
                   createInstance(Ci.nsIMIMEInputStream);
                postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
                postData.addContentLength = true;
                postData.setData(stringStream);
                winUtils.getMostRecentBrowserWindow().loadURI("http://localhost:8090/Whatever/PdfServlet", null, postData, null);
    

    Or I could open a new window, but I didn't like that:

        winUtils.openDialog({
            args: ["http://localhost:8090/Whatever/PdfServlet", null, null, postData]
        });