Skip to content

Commit 182b0c5

Browse files
author
daserge
committed
CB-9837 Add data URI support to file-transfer upload on iOS
Adds iOS and Windows implementation; mention in the docs Adds corresponding tests Increases spec.35 timeout for Windows Phone 8.1 case as it contains 2 download operations
1 parent a9470ff commit 182b0c5

File tree

4 files changed

+340
-92
lines changed

4 files changed

+340
-92
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ multi-part POST or PUT request, and to download files as well.
7474

7575
__Parameters__:
7676

77-
- __fileURL__: Filesystem URL representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device. (See [Backwards Compatibility Notes] below)
77+
- __fileURL__: Filesystem URL representing the file on the device or a [data: URI](https://en.wikipedia.org/wiki/Data_URI_scheme). For backwards compatibility, this can also be the full path of the file on the device. (See [Backwards Compatibility Notes](#backwards-compatibility-notes) below)
7878

7979
- __server__: URL of the server to receive the file, as encoded by `encodeURI()`.
8080

src/ios/CDVFileTransfer.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ - (NSURLRequest*)requestForUploadCommand:(CDVInvokedUrlCommand*)command fileData
244244
int numChunks = sizeof(chunks) / sizeof(chunks[0]);
245245

246246
for (int i = 0; i < numChunks; ++i) {
247+
// Allow uploading of an empty file
248+
if (chunks[i].length == 0) {
249+
continue;
250+
}
251+
247252
CFIndex result = WriteDataToStream(chunks[i], writeStream);
248253
if (result <= 0) {
249254
break;
@@ -297,6 +302,28 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
297302
NSString* server = [command argumentAtIndex:1];
298303
NSError* __autoreleasing err = nil;
299304

305+
if ([source hasPrefix:@"data:"] && [source rangeOfString:@"base64"].location != NSNotFound) {
306+
NSRange commaRange = [source rangeOfString: @","];
307+
if (commaRange.location == NSNotFound) {
308+
// Return error is there is no comma
309+
__weak CDVFileTransfer* weakSelf = self;
310+
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[weakSelf createFileTransferError:INVALID_URL_ERR AndSource:source AndTarget:server]];
311+
[weakSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
312+
return;
313+
}
314+
315+
if (commaRange.location + 1 > source.length - 1) {
316+
// Init as an empty data
317+
NSData *fileData = [[NSData alloc] init];
318+
[self uploadData:fileData command:command];
319+
return;
320+
}
321+
322+
NSData *fileData = [[NSData alloc] initWithBase64EncodedString:[source substringFromIndex:(commaRange.location + 1)] options:NSDataBase64DecodingIgnoreUnknownCharacters];
323+
[self uploadData:fileData command:command];
324+
return;
325+
}
326+
300327
CDVFilesystemURL *sourceURL = [CDVFilesystemURL fileSystemURLWithString:source];
301328
NSObject<CDVFileSystem> *fs;
302329
if (sourceURL) {

0 commit comments

Comments
 (0)