PhoneGap的文件传输使用HTTP基本身份验证 [英] PhoneGap FileTransfer with HTTP basic authentication
问题描述
我试图使用文件传输方法从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);
<一个href=\"http://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=blob_plain;f=framework/src/org/apache/cordova/FileTransfer.java;hb=c341cf0d47c4c24e097985fe941adabfbd1e30b7\">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') };
这篇关于PhoneGap的文件传输使用HTTP基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!