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

北城以北 提交于 2019-12-11 00:42:34

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/19386049/how-to-make-firefox-handle-the-result-of-a-post-request-with-firefox-addon-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!