9

我正在尝试使用FileTransfer 方法将文件从 PhoneGap 上传到服务器。我需要为此上传启用 HTTP 基本身份验证。

以下是相关代码:

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

查看 PhoneGap 源代码,我似乎可以通过在“参数”列表中包含“标题”来指定授权标题,就像我在上面所做的那样:

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

但是,这似乎并没有真正添加标题。

那么:对于 iPhone 和 Android,有没有办法使用 PhoneGap 的 FileTransfer 进行 HTTP 基本身份验证?

4

3 回答 3

10

您可以通过将它们添加到选项而不是像这样的参数来添加自定义标题:

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

options.headers = {'Authorization': authHeaderValue('Bob', '1234') };
于 2013-01-14T03:37:15.443 回答
0

您可以自己创建授权标头。但您也可以像这样在 url 中输入凭据:

var username = "test", password = "pass";     
var uri = encodeURI("http://"+username + ':' + password +"@localhost:8000/api/upload");

有关实现,请参见 FileTransfer.js(第 45 行):

function getBasicAuthHeader(urlString) {
var header =  null;


// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem

if (window.btoa) {
    var credentials = getUrlCredentials(urlString);
    if (credentials) {
        var authHeader = "Authorization";
        var authHeaderValue = "Basic " + window.btoa(credentials);

        header = {
            name : authHeader,
            value : authHeaderValue
        };
    }
}

return header;
}
于 2017-05-03T15:13:15.910 回答
0

headers 数组的正确位置是选项的直接子级。选项->标题。不是选项-> 参数-> 标题。这是一个例子:

//**************************************************************
//Variables used below:
//1 - image_name: contains the actual name of the image file.
//2 - token: contains authorization token. In my case, JWT.
//3 - UPLOAD_URL: URL to which the file will be uploaded.
//4 - image_full_path - Full path for the picture to be uploaded.
//***************************************************************
var options = {
  fileKey: "file",
  fileName: 'picture',
  chunkedMode: false,
  mimeType: "multipart/form-data",
  params : {'fileName': image_name}
};

var headers = {'Authorization':token};

//Here is the magic!
options.headers = headers;
//NOTE: I creaed a separate object for headers to better exemplify what
// is going on here. Obviously you can simply add the header entry
// directly to options object above.

$cordovaFileTransfer.upload(UPLOAD_URL, image_full_path, options).then(
   function(result) {
      //do whatever with the result here.
 });

这是官方文档:https ://github.com/apache/cordova-plugin-file-transfer

于 2017-05-02T02:46:27.073 回答