PhoneGap的文件传输使用HTTP基本身份验证

问题描述:

我试图使用文件传输方法从PhoneGap的文件上载到服务器。我需要为这个上传启用HTTP基本身份验证。

I'm attempting to upload a file from PhoneGap to a server using the FileTransfer method. I need HTTP basic auth to be enabled for this upload.

下面是有关的code:

Here's the relevant code:

    var options = new FileUploadOptions({
        fileKey: "file",
        params: {
            id: my_id,
            headers: { 'Authorization': _make_authstr() }
        }
    });
    var ft = new FileTransfer();
    ft.upload(image, 'http://locahost:8000/api/upload', success, error, options);

Looking在PhoneGap的源$ C ​​$ C 看来我可以通过在PARAMS名单头指定授权头,因为我之前所做的那样:

Looking over the PhoneGap source code it appears that I can specify the authorization header by including "headers" in the "params" list as I've done above:

      JSONObject headers = params.getJSONObject("headers");
      for (Iterator iter = headers.keys(); iter.hasNext();)
      {
        String headerKey = iter.next().toString();
        conn.setRequestProperty(headerKey, headers.getString(headerKey));
      }

不过,这似乎并没有实际添加标题。

However, this doesn't seem to actually add the header.

所以:有没有办法做到HTTP使用的PhoneGap的文件传输基本身份验证,为iPhone和Android

So: is there a way to do HTTP basic auth with PhoneGap's FileTransfer, for both iPhone and Android?

您可以通过将其添加到选项,而不是添加自定义页眉在 PARAMS 像这样:

You can add custom headers by adding them to the options rather than the params like so:

authHeaderValue = function(username, password) {
    var tok = username + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
};

options.headers = {'Authorization': authHeaderValue('Bob', '1234') };