当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to upload a file from the user's system using firefox addon sdk?

问题描述:

I am new to Javascript and Firefox addon programming.

I am making an add-on with the Add-on SDK that logs some information about user browsing activities. I use the file API with some hard coded file names.

Now I want to analyze those log files on the server side but I don't want the user to go through the process of locating the files and uploading them manually. I want to provide a button to the user; onclicking which those files will be automatically uploaded to the server.

This automatic uploading functionality is quiet vital for the entire project since it is a study oriented project and it will be very hard to get data from the user if he'll have to upload it manually.

网友答案:

Actual file upload (via multipart/form-data) is rather complicated to achieve but since you are controlling the server side - why not accept a regular POST request where the file data is in one of the form fields? So your code sending data to the server could look like this:

var file = require("sdk/io/file");
var {Request} = require("sdk/request");

var params = {};
params.log = file.read("c:\\foo\\bar.log", "b");
params.additional = "some additional form field";

Request({
  url: "https://example.com/uploadlog",
  content: params,
  onComplete: function(response)
  {
    console.log("Request sent");
  }
}).post();

And you can put up a button anywhere that will trigger this code.

分享给朋友:
您可能感兴趣的文章:
随机阅读: