[
https://issues.apache.org/jira/browse/CB-11067?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15301933#comment-15301933
]
ASF GitHub Bot commented on CB-11067:
-------------------------------------
Github user oddcb commented on the pull request:
https://github.com/apache/cordova-plugin-file-transfer/pull/140#issuecomment-221839381
Thanks for you reply @daserge
So I'm not very into how file uploads are done properly, but the problem I
fixed for our api here was adding the Content-Length into the code block
starting at:
https://github.com/apache/cordova-plugin-file-transfer/blob/9347606dd33fe07ea36799b4dd28804019c68835/src/android/FileTransfer.java#L406
Result:
```java
beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
beforeData.append("Content-Disposition: form-data;
name=\"").append(fileKey).append("\";");
beforeData.append("
filename=\"").append(fileName).append('"').append(LINE_END);
if(filesize > -1)
beforeData.append("Content-Length:
").append(filesize).append(LINE_END);
beforeData.append("Content-Type:
").append(mimeType).append(LINE_END).append(LINE_END);
byte[] beforeDataBytes =
beforeData.toString().getBytes("UTF-8");
byte[] tailParamsBytes = (LINE_END + LINE_START +
BOUNDARY + LINE_START + LINE_END).getBytes("UTF-8");
int stringLength = beforeDataBytes.length +
tailParamsBytes.length;
```
As far as I can tell it is also what happens for iOS in
https://github.com/apache/cordova-plugin-file-transfer/blob/9347606dd33fe07ea36799b4dd28804019c68835/src/ios/CDVFileTransfer.m#L222
``` [postBodyBeforeFile appendData:[[NSString
stringWithFormat:@"Content-Length: %ld\r\n\r\n", (long)[fileData length]]
dataUsingEncoding:NSUTF8StringEncoding]];```
History for us is just that our ios app was working with the file upload
plugin as is, but when we added the android app with the plugin it did not send
Content-Length as iOS did and hence our file upload API started to scream. We
are using Jersey in our backend.
As far as I can tell your opinion is that this added code should not be
required interacting with a standard api?
> Content-Length missing in multipart form upload header on Android
> -----------------------------------------------------------------
>
> Key: CB-11067
> URL: https://issues.apache.org/jira/browse/CB-11067
> Project: Apache Cordova
> Issue Type: Bug
> Components: Plugin File Transfer
> Environment: Plugin version 1.5.0
> chunked = false
> Reporter: Odd Christer Brovig
> Labels: Android, triaged
>
> On iOS Content-Length is sent as part of header for multipart form. On
> Android only Content-Disposition and Content-Type is sent.
> Is there a special reason for this? I see that the file size is read after
> the beforeData is converted to bytes.
> From FileTransfer.java:
> {code:java}
>
> beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
> beforeData.append("Content-Disposition: form-data;
> name=\"").append(fileKey).append("\";");
> beforeData.append("
> filename=\"").append(fileName).append('"').append(LINE_END);
> beforeData.append("Content-Type:
> ").append(mimeType).append(LINE_END).append(LINE_END);
> byte[] beforeDataBytes =
> beforeData.toString().getBytes("UTF-8");
> byte[] tailParamsBytes = (LINE_END + LINE_START +
> BOUNDARY + LINE_START + LINE_END).getBytes("UTF-8");
> {code}
> From CDVFileTransfer.m:
> {code}
> [postBodyBeforeFile appendData:formBoundaryData];
> [postBodyBeforeFile appendData:[[NSString
> stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";
> filename=\"%@\"\r\n", fileKey, fileName]
> dataUsingEncoding:NSUTF8StringEncoding]];
> if (mimeType != nil) {
> [postBodyBeforeFile appendData:[[NSString
> stringWithFormat:@"Content-Type: %@\r\n", mimeType]
> dataUsingEncoding:NSUTF8StringEncoding]];
> }
> [postBodyBeforeFile appendData:[[NSString
> stringWithFormat:@"Content-Length: %ld\r\n\r\n", (long)[fileData length]]
> dataUsingEncoding:NSUTF8StringEncoding]];
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]