[ 
https://issues.apache.org/jira/browse/CB-12745?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16261749#comment-16261749
 ] 

ASF GitHub Bot commented on CB-12745:
-------------------------------------

maverickmishra closed pull request #178: CB-12745: Adding support for non 
base64 encoded data-URIs
URL: https://github.com/apache/cordova-plugin-file-transfer/pull/178
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m
index ab59012..46efce9 100644
--- a/src/ios/CDVFileTransfer.m
+++ b/src/ios/CDVFileTransfer.m
@@ -308,7 +308,7 @@ - 
(void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
     NSString* server = [command argumentAtIndex:1];
     NSError* __autoreleasing err = nil;
 
-    if ([source hasPrefix:@"data:"] && [source 
rangeOfString:@"base64"].location != NSNotFound) {
+    if ([source hasPrefix:@"data:"]) {
         NSRange commaRange = [source rangeOfString: @","];
         if (commaRange.location == NSNotFound) {
             // Return error is there is no comma
@@ -325,8 +325,17 @@ - 
(void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
             return;
         }
 
-        NSData *fileData = [[NSData alloc] initWithBase64EncodedString:[source 
substringFromIndex:(commaRange.location + 1)] 
options:NSDataBase64DecodingIgnoreUnknownCharacters];
-        [self uploadData:fileData command:command];
+        // check if source is base64 encoded
+        if ([source rangeOfString:@"base64"].location != NSNotFound) {
+            NSData *fileData = [[NSData alloc] 
initWithBase64EncodedString:[source substringFromIndex:(commaRange.location + 
1)] options:NSDataBase64DecodingIgnoreUnknownCharacters];
+            [self uploadData:fileData command:command];
+        }
+        // if not, apply urldecode and encode it to NSData using UTF8
+        else {
+            NSData *fileData = [[[source 
substringFromIndex:(commaRange.location + 1)] 
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
dataUsingEncoding:NSUTF8StringEncoding];
+            [self uploadData:fileData command:command];
+        }
+
         return;
     }
 
diff --git a/src/windows/FileTransferProxy.js b/src/windows/FileTransferProxy.js
index 5d1363f..24866ff 100644
--- a/src/windows/FileTransferProxy.js
+++ b/src/windows/FileTransferProxy.js
@@ -191,7 +191,13 @@ exec(win, fail, 'FileTransfer', 'upload',
             return;
         }
 
-        if (filePath.indexOf("data:") === 0 && filePath.indexOf("base64") !== 
-1) {
+        if (filePath.indexOf("data:") === 0) {
+            // check if we have base64 encoded data
+            var isBase64 = false;
+            if (filePath.indexOf("base64") !== -1) {
+                isBase64 = true;
+            }
+
             // 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());
@@ -241,10 +247,18 @@ exec(win, fail, 'FileTransfer', 'upload',
                 uploader.setRequestHeader("Content-Type", 
"multipart/form-data; boundary=" + BOUNDARY);
                 writer.writeString(multipartParams);
                 writer.writeString(multipartFile);
-                writer.writeBytes(stringToByteArray(fileDataString));
+                if (isBase64) {
+                    writer.writeBytes(stringToByteArray(fileDataString));
+                } else {
+                    writer.writeString(decodeURIComponent(fileDataString));
+                }
                 writer.writeString(bound);
             } else {
-                writer.writeBytes(stringToByteArray(fileDataString));
+                if (isBase64) {
+                    writer.writeBytes(stringToByteArray(fileDataString));
+                } else {
+                    writer.writeString(decodeURIComponent(fileDataString));
+                }
             }
 
             var stream;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Add suppport for data: URIs which are not Base64 encoded
> --------------------------------------------------------
>
>                 Key: CB-12745
>                 URL: https://issues.apache.org/jira/browse/CB-12745
>             Project: Apache Cordova
>          Issue Type: Improvement
>          Components: cordova-plugin-file-transfer (DEPRECATED)
>         Environment: iOS, Windows
>            Reporter: Wolfgang Koller
>            Assignee: Wolfgang Koller
>
> Currently the FileUpload implementation on iOS expects the data URI content 
> to be Base64 encoded. In order to be more compatible to the Android 
> implementation, it should be possible to also use non Base64 encoded content.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to