Return-Path: X-Original-To: apmail-cordova-dev-archive@www.apache.org Delivered-To: apmail-cordova-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BF09E183FC for ; Thu, 10 Dec 2015 06:56:34 +0000 (UTC) Received: (qmail 36464 invoked by uid 500); 10 Dec 2015 06:56:34 -0000 Delivered-To: apmail-cordova-dev-archive@cordova.apache.org Received: (qmail 36425 invoked by uid 500); 10 Dec 2015 06:56:34 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 36414 invoked by uid 99); 10 Dec 2015 06:56:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Dec 2015 06:56:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id DFE30E0AC0; Thu, 10 Dec 2015 06:56:33 +0000 (UTC) From: daserge To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-plugin-file-transfer pull request: CB-9837 Add data URI su... Content-Type: text/plain Message-Id: <20151210065633.DFE30E0AC0@git1-us-west.apache.org> Date: Thu, 10 Dec 2015 06:56:33 +0000 (UTC) Github user daserge commented on a diff in the pull request: https://github.com/apache/cordova-plugin-file-transfer/pull/122#discussion_r47194431 --- Diff: src/windows/FileTransferProxy.js --- @@ -89,19 +179,141 @@ exec(win, fail, 'FileTransfer', 'upload', var isMultipart = typeof headers["Content-Type"] === 'undefined'; if (!filePath || (typeof filePath !== 'string')) { - errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR,null,server)); + errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, null, server)); return; } - if (filePath.substr(0, 8) === "file:///") { - filePath = appData.localFolder.path + filePath.substr(8).split("/").join("\\"); - } else if (filePath.indexOf('ms-appdata:///') === 0) { - // Handle 'ms-appdata' scheme - filePath = filePath.replace('ms-appdata:///local', appData.localFolder.path) - .replace('ms-appdata:///temp', appData.temporaryFolder.path); - } else if (filePath.indexOf('cdvfile://') === 0) { - filePath = filePath.replace('cdvfile://localhost/persistent', appData.localFolder.path) - .replace('cdvfile://localhost/temporary', appData.temporaryFolder.path); + if (filePath.indexOf("data:") === 0 && filePath.indexOf("base64") !== -1) { + // First a DataWriter object is created, backed by an in-memory stream where + // the data will be stored. + var writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream()); + writer.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8; + writer.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian; + + var commaIndex = filePath.indexOf(","); + if (commaIndex === -1) { + errorCallback(new FTErr(FTErr.INVALID_URL_ERR, fileName, server, null, null, "No comma in data: URI")); + return; + } + + // Create internal download operation object + fileTransferOps[uploadId] = new FileTransferOperation(FileTransferOperation.PENDING, null); + + var fileDataString = filePath.substr(commaIndex + 1); + + function stringToByteArray(str) { + var byteCharacters = atob(str); + var byteNumbers = new Array(byteCharacters.length); + for (var i = 0; i < byteCharacters.length; i++) { + byteNumbers[i] = byteCharacters.charCodeAt(i); + } + return new Uint8Array(byteNumbers); + }; + + // setting request headers for uploader + var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader(); + uploader.method = httpMethod; + for (var header in headers) { + if (headers.hasOwnProperty(header)) { + uploader.setRequestHeader(header, headers[header]); + } + } + + if (isMultipart) { + // adding params supplied to request payload + var multipartParams = ''; + for (var key in params) { + if (params.hasOwnProperty(key)) { + multipartParams += LINE_START + BOUNDARY + LINE_END; + multipartParams += "Content-Disposition: form-data; name=\"" + key + "\""; --- End diff -- None I've found for Windows and `createUploadFromStreamAsync`. I think the current approach works as it is consistent with [Android](https://github.com/apache/cordova-plugin-file-transfer/blob/a9470ff1cc313be542f625f857d33fb14a520d6f/src/android/FileTransfer.java#L385-L411) and [iOS code](https://github.com/apache/cordova-plugin-file-transfer/blob/a9470ff1cc313be542f625f857d33fb14a520d6f/src/ios/CDVFileTransfer.m#L197-L225) for multipart forms and allows a fine tuning. Alternative could be saving to a temporary file and using `BackgroundTransferContentPart` but it will be less efficient. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org For additional commands, e-mail: dev-help@cordova.apache.org