diff --git a/src/wp/FileTransfer.cs b/src/wp/FileTransfer.cs index 11db284..8e234cd 100644 --- a/src/wp/FileTransfer.cs +++ b/src/wp/FileTransfer.cs @@ -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 } } + /// + /// Represents a request header passed from Javascript to upload/download operations + /// + [DataContract] + protected struct Header + { + [DataMember(Name = "name")] + public string Name; + + [DataMember(Name = "value")] + public string Value; + } + /// /// Upload options /// @@ -311,34 +326,14 @@ namespace WPCordovaClassLib.Cordova.Commands { try { - Dictionary result = new Dictionary(); - - 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(split[0]); - split[1] = JSON.JsonHelper.Deserialize(split[1]); - result[split[0]] = split[1]; - } - } - return result; + return JsonHelper.Deserialize(jsonHeaders) + .ToDictionary(header => header.Name, header => header.Value); } catch (Exception) { Debug.WriteLine("Failed to parseHeaders from string :: " + jsonHeaders); } - return null; + return new Dictionary(); } public void download(string options) diff --git a/www/FileTransfer.js b/www/FileTransfer.js index 17fb782..5db3fec 100644 --- a/www/FileTransfer.js +++ b/www/FileTransfer.js @@ -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) {