mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2026-07-13 00:10:15 +08:00
CB-8721 Fixes incorrect headers and upload params parsing on wp8
This commit is contained in:
+18
-23
@@ -16,11 +16,13 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.IsolatedStorage;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Windows;
|
||||
using System.Security;
|
||||
using System.Diagnostics;
|
||||
using WPCordovaClassLib.Cordova.JSON;
|
||||
|
||||
namespace WPCordovaClassLib.Cordova.Commands
|
||||
{
|
||||
@@ -209,6 +211,19 @@ namespace WPCordovaClassLib.Cordova.Commands
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request header passed from Javascript to upload/download operations
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
protected struct Header
|
||||
{
|
||||
[DataMember(Name = "name")]
|
||||
public string Name;
|
||||
|
||||
[DataMember(Name = "value")]
|
||||
public string Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload options
|
||||
/// </summary>
|
||||
@@ -311,34 +326,14 @@ namespace WPCordovaClassLib.Cordova.Commands
|
||||
{
|
||||
try
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
|
||||
string temp = jsonHeaders.StartsWith("{") ? jsonHeaders.Substring(1) : jsonHeaders;
|
||||
temp = temp.EndsWith("}") ? temp.Substring(0, temp.Length - 1) : temp;
|
||||
|
||||
string[] strHeaders = temp.Split(',');
|
||||
for (int n = 0; n < strHeaders.Length; n++)
|
||||
{
|
||||
// we need to use indexOf in order to WP7 compatible
|
||||
int splitIndex = strHeaders[n].IndexOf(':');
|
||||
if (splitIndex > 0)
|
||||
{
|
||||
string[] split = new string[2];
|
||||
split[0] = strHeaders[n].Substring(0, splitIndex);
|
||||
split[1] = strHeaders[n].Substring(splitIndex + 1);
|
||||
|
||||
split[0] = JSON.JsonHelper.Deserialize<string>(split[0]);
|
||||
split[1] = JSON.JsonHelper.Deserialize<string>(split[1]);
|
||||
result[split[0]] = split[1];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return JsonHelper.Deserialize<Header[]>(jsonHeaders)
|
||||
.ToDictionary(header => header.Name, header => header.Value);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.WriteLine("Failed to parseHeaders from string :: " + jsonHeaders);
|
||||
}
|
||||
return null;
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public void download(string options)
|
||||
|
||||
@@ -63,6 +63,20 @@ function getBasicAuthHeader(urlString) {
|
||||
return header;
|
||||
}
|
||||
|
||||
function convertHeadersToArray(headers) {
|
||||
var result = [];
|
||||
for (var header in headers) {
|
||||
if (headers.hasOwnProperty(header)) {
|
||||
var headerValue = headers[header];
|
||||
result.push({
|
||||
name: header,
|
||||
value: headerValue.toString()
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var idCounter = 0;
|
||||
|
||||
/**
|
||||
@@ -125,6 +139,11 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
||||
}
|
||||
}
|
||||
|
||||
if (cordova.platformId === "windowsphone") {
|
||||
headers = headers && convertHeadersToArray(headers);
|
||||
params = params && convertHeadersToArray(params);
|
||||
}
|
||||
|
||||
var fail = errorCallback && function(e) {
|
||||
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
|
||||
errorCallback(error);
|
||||
@@ -170,6 +189,10 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
||||
headers = options.headers || null;
|
||||
}
|
||||
|
||||
if (cordova.platformId === "windowsphone" && headers) {
|
||||
headers = convertHeadersToArray(headers);
|
||||
}
|
||||
|
||||
var win = function(result) {
|
||||
if (typeof result.lengthComputable != "undefined") {
|
||||
if (self.onprogress) {
|
||||
|
||||
Reference in New Issue
Block a user