mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2024-10-06 07:42:20 +08:00
Merge branch 'CB-6503'
This commit is contained in:
commit
de55dc7f84
@ -57,7 +57,7 @@ Although in the global scope, they are not available until after the `deviceread
|
|||||||
# FileTransfer
|
# FileTransfer
|
||||||
|
|
||||||
The `FileTransfer` object provides a way to upload files using an HTTP
|
The `FileTransfer` object provides a way to upload files using an HTTP
|
||||||
multi-part POST request, and to download files as well.
|
multi-part POST or PUT request, and to download files as well.
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
@ -91,8 +91,9 @@ __Parameters__:
|
|||||||
- __mimeType__: The mime type of the data to upload. Defaults to `image/jpeg`. (DOMString)
|
- __mimeType__: The mime type of the data to upload. Defaults to `image/jpeg`. (DOMString)
|
||||||
- __params__: A set of optional key/value pairs to pass in the HTTP request. (Object)
|
- __params__: A set of optional key/value pairs to pass in the HTTP request. (Object)
|
||||||
- __chunkedMode__: Whether to upload the data in chunked streaming mode. Defaults to `true`. (Boolean)
|
- __chunkedMode__: Whether to upload the data in chunked streaming mode. Defaults to `true`. (Boolean)
|
||||||
- __headers__: A map of header name/header values. Use an array to specify more than one value. (Object)
|
- __headers__: A map of header name/header values. Use an array to specify more than one value. On iOS, FireOS, and Android, if a header named Content-Type is present, multipart form data will NOT be used. (Object)
|
||||||
|
- __httpMethod__: The HTTP method to use e.g. POST or PUT. Defaults to `POST`. (DOMString)
|
||||||
|
|
||||||
- __trustAllHosts__: Optional parameter, defaults to `false`. If set to `true`, it accepts all security certificates. This is useful since Android rejects self-signed security certificates. Not recommended for production use. Supported on Android and iOS. _(boolean)_
|
- __trustAllHosts__: Optional parameter, defaults to `false`. If set to `true`, it accepts all security certificates. This is useful since Android rejects self-signed security certificates. Not recommended for production use. Supported on Android and iOS. _(boolean)_
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
@ -315,8 +315,12 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
|
|
||||||
// Use a post method.
|
// Use a post method.
|
||||||
conn.setRequestMethod(httpMethod);
|
conn.setRequestMethod(httpMethod);
|
||||||
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
||||||
|
|
||||||
|
// if we specified a Content-Type header, don't do multipart form upload
|
||||||
|
boolean multipartFormUpload = (headers == null) || !headers.has("Content-Type");
|
||||||
|
if (multipartFormUpload) {
|
||||||
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
||||||
|
}
|
||||||
// Set the cookies on the response
|
// Set the cookies on the response
|
||||||
String cookie = AmazonCookieManager.getInstance().getCookie(target);
|
String cookie = AmazonCookieManager.getInstance().getCookie(target);
|
||||||
if (cookie != null) {
|
if (cookie != null) {
|
||||||
@ -362,7 +366,9 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
|
|
||||||
int stringLength = beforeDataBytes.length + tailParamsBytes.length;
|
int stringLength = beforeDataBytes.length + tailParamsBytes.length;
|
||||||
if (readResult.length >= 0) {
|
if (readResult.length >= 0) {
|
||||||
fixedLength = (int)readResult.length + stringLength;
|
fixedLength = (int)readResult.length;
|
||||||
|
if (multipartFormUpload)
|
||||||
|
fixedLength += stringLength;
|
||||||
progress.setLengthComputable(true);
|
progress.setLengthComputable(true);
|
||||||
progress.setTotal(fixedLength);
|
progress.setTotal(fixedLength);
|
||||||
}
|
}
|
||||||
@ -393,10 +399,12 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
context.currentOutputStream = sendStream;
|
context.currentOutputStream = sendStream;
|
||||||
}
|
}
|
||||||
//We don't want to change encoding, we just want this to write for all Unicode.
|
|
||||||
sendStream.write(beforeDataBytes);
|
if (multipartFormUpload) {
|
||||||
totalBytes += beforeDataBytes.length;
|
//We don't want to change encoding, we just want this to write for all Unicode.
|
||||||
|
sendStream.write(beforeDataBytes);
|
||||||
|
totalBytes += beforeDataBytes.length;
|
||||||
|
}
|
||||||
// create a buffer of maximum size
|
// create a buffer of maximum size
|
||||||
int bytesAvailable = readResult.inputStream.available();
|
int bytesAvailable = readResult.inputStream.available();
|
||||||
int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
|
int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
|
||||||
@ -425,9 +433,11 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
context.sendPluginResult(progressResult);
|
context.sendPluginResult(progressResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send multipart form data necessary after file data...
|
if (multipartFormUpload) {
|
||||||
sendStream.write(tailParamsBytes);
|
// send multipart form data necessary after file data...
|
||||||
totalBytes += tailParamsBytes.length;
|
sendStream.write(tailParamsBytes);
|
||||||
|
totalBytes += tailParamsBytes.length;
|
||||||
|
}
|
||||||
sendStream.flush();
|
sendStream.flush();
|
||||||
} finally {
|
} finally {
|
||||||
safeClose(readResult.inputStream);
|
safeClose(readResult.inputStream);
|
||||||
|
@ -341,8 +341,13 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
|
|
||||||
// Use a post method.
|
// Use a post method.
|
||||||
conn.setRequestMethod(httpMethod);
|
conn.setRequestMethod(httpMethod);
|
||||||
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
||||||
|
// if we specified a Content-Type header, don't do multipart form upload
|
||||||
|
boolean multipartFormUpload = (headers == null) || !headers.has("Content-Type");
|
||||||
|
if (multipartFormUpload) {
|
||||||
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
||||||
|
}
|
||||||
|
|
||||||
// Set the cookies on the response
|
// Set the cookies on the response
|
||||||
String cookie = getCookies(target);
|
String cookie = getCookies(target);
|
||||||
|
|
||||||
@ -389,7 +394,9 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
|
|
||||||
int stringLength = beforeDataBytes.length + tailParamsBytes.length;
|
int stringLength = beforeDataBytes.length + tailParamsBytes.length;
|
||||||
if (readResult.length >= 0) {
|
if (readResult.length >= 0) {
|
||||||
fixedLength = (int)readResult.length + stringLength;
|
fixedLength = (int)readResult.length;
|
||||||
|
if (multipartFormUpload)
|
||||||
|
fixedLength += stringLength;
|
||||||
progress.setLengthComputable(true);
|
progress.setLengthComputable(true);
|
||||||
progress.setTotal(fixedLength);
|
progress.setTotal(fixedLength);
|
||||||
}
|
}
|
||||||
@ -420,10 +427,13 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
context.connection = conn;
|
context.connection = conn;
|
||||||
}
|
}
|
||||||
//We don't want to change encoding, we just want this to write for all Unicode.
|
|
||||||
sendStream.write(beforeDataBytes);
|
if (multipartFormUpload) {
|
||||||
totalBytes += beforeDataBytes.length;
|
//We don't want to change encoding, we just want this to write for all Unicode.
|
||||||
|
sendStream.write(beforeDataBytes);
|
||||||
|
totalBytes += beforeDataBytes.length;
|
||||||
|
}
|
||||||
|
|
||||||
// create a buffer of maximum size
|
// create a buffer of maximum size
|
||||||
int bytesAvailable = readResult.inputStream.available();
|
int bytesAvailable = readResult.inputStream.available();
|
||||||
int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
|
int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
|
||||||
@ -452,9 +462,11 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
context.sendPluginResult(progressResult);
|
context.sendPluginResult(progressResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send multipart form data necessary after file data...
|
if (multipartFormUpload) {
|
||||||
sendStream.write(tailParamsBytes);
|
// send multipart form data necessary after file data...
|
||||||
totalBytes += tailParamsBytes.length;
|
sendStream.write(tailParamsBytes);
|
||||||
|
totalBytes += tailParamsBytes.length;
|
||||||
|
}
|
||||||
sendStream.flush();
|
sendStream.flush();
|
||||||
} finally {
|
} finally {
|
||||||
safeClose(readResult.inputStream);
|
safeClose(readResult.inputStream);
|
||||||
|
@ -183,8 +183,12 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
|
|||||||
[req setHTTPShouldHandleCookies:NO];
|
[req setHTTPShouldHandleCookies:NO];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kFormBoundary];
|
// if we specified a Content-Type header, don't do multipart form upload
|
||||||
[req setValue:contentType forHTTPHeaderField:@"Content-Type"];
|
BOOL multipartFormUpload = [headers objectForKey:@"Content-Type"] == nil;
|
||||||
|
if (multipartFormUpload) {
|
||||||
|
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kFormBoundary];
|
||||||
|
[req setValue:contentType forHTTPHeaderField:@"Content-Type"];
|
||||||
|
}
|
||||||
[self applyRequestHeaders:headers toRequest:req];
|
[self applyRequestHeaders:headers toRequest:req];
|
||||||
|
|
||||||
NSData* formBoundaryData = [[NSString stringWithFormat:@"--%@\r\n", kFormBoundary] dataUsingEncoding:NSUTF8StringEncoding];
|
NSData* formBoundaryData = [[NSString stringWithFormat:@"--%@\r\n", kFormBoundary] dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
@ -220,7 +224,11 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
|
|||||||
DLog(@"fileData length: %d", [fileData length]);
|
DLog(@"fileData length: %d", [fileData length]);
|
||||||
NSData* postBodyAfterFile = [[NSString stringWithFormat:@"\r\n--%@--\r\n", kFormBoundary] dataUsingEncoding:NSUTF8StringEncoding];
|
NSData* postBodyAfterFile = [[NSString stringWithFormat:@"\r\n--%@--\r\n", kFormBoundary] dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
|
|
||||||
long long totalPayloadLength = [postBodyBeforeFile length] + [fileData length] + [postBodyAfterFile length];
|
long long totalPayloadLength = [fileData length];
|
||||||
|
if (multipartFormUpload) {
|
||||||
|
totalPayloadLength += [postBodyBeforeFile length] + [postBodyAfterFile length];
|
||||||
|
}
|
||||||
|
|
||||||
[req setValue:[[NSNumber numberWithLongLong:totalPayloadLength] stringValue] forHTTPHeaderField:@"Content-Length"];
|
[req setValue:[[NSNumber numberWithLongLong:totalPayloadLength] stringValue] forHTTPHeaderField:@"Content-Length"];
|
||||||
|
|
||||||
if (chunkedMode) {
|
if (chunkedMode) {
|
||||||
@ -231,14 +239,18 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
|
|||||||
|
|
||||||
[self.commandDelegate runInBackground:^{
|
[self.commandDelegate runInBackground:^{
|
||||||
if (CFWriteStreamOpen(writeStream)) {
|
if (CFWriteStreamOpen(writeStream)) {
|
||||||
NSData* chunks[] = {postBodyBeforeFile, fileData, postBodyAfterFile};
|
if (multipartFormUpload) {
|
||||||
int numChunks = sizeof(chunks) / sizeof(chunks[0]);
|
NSData* chunks[] = { postBodyBeforeFile, fileData, postBodyAfterFile };
|
||||||
|
int numChunks = sizeof(chunks) / sizeof(chunks[0]);
|
||||||
|
|
||||||
for (int i = 0; i < numChunks; ++i) {
|
for (int i = 0; i < numChunks; ++i) {
|
||||||
CFIndex result = WriteDataToStream(chunks[i], writeStream);
|
CFIndex result = WriteDataToStream(chunks[i], writeStream);
|
||||||
if (result <= 0) {
|
if (result <= 0) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
WriteDataToStream(fileData, writeStream);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
NSLog(@"FileTransfer: Failed to open writeStream");
|
NSLog(@"FileTransfer: Failed to open writeStream");
|
||||||
@ -247,9 +259,13 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream)
|
|||||||
CFRelease(writeStream);
|
CFRelease(writeStream);
|
||||||
}];
|
}];
|
||||||
} else {
|
} else {
|
||||||
[postBodyBeforeFile appendData:fileData];
|
if (multipartFormUpload) {
|
||||||
[postBodyBeforeFile appendData:postBodyAfterFile];
|
[postBodyBeforeFile appendData:fileData];
|
||||||
[req setHTTPBody:postBodyBeforeFile];
|
[postBodyBeforeFile appendData:postBodyAfterFile];
|
||||||
|
[req setHTTPBody:postBodyBeforeFile];
|
||||||
|
} else {
|
||||||
|
[req setHTTPBody:fileData];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user