When uploading a file (in this case a jpg image) using PhoneGap's File-Transfer API, the FileTransferOptions only ends up the "fileName" and "name" in the multipart post body, even though it states that I can use key:value pairs via options.params.
What I am doing currently is appending many different terms to the "name" option to receive my information serverside, however I would have liked to transfer them as key:value pairs. In the FileTransfer.java file in the plugin folder (for android) I discovered that the params were assigned into the name field, as well as the fileKey. Subsequently, whenever I try to assign params using the API as a reference, the multipart POST fails.
What I am doing currently is appending many different terms to the "name" option to receive my information serverside, however I would have liked to transfer them as key:value pairs. In the FileTransfer.java file in the plugin folder (for android) I discovered that the params were assigned into the name field, as well as the fileKey. Subsequently, whenever I try to assign params using the API as a reference, the multipart POST fails.
- 4 Posts
- 0 Reply Likes
Posted 4 years ago
- 4 Posts
- 0 Reply Likes
This is the piece of code within FileTransfer.java in question. As you can see, name is defined twice.
StringBuilder beforeData = new StringBuilder();
try {
for (Iterator iter = params.keys(); iter.hasNext();) {
Object key = iter.next();
if(!String.valueOf(key).equals("headers"))
{
beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
beforeData.append("Content-Disposition: form-data; name=\"").append(key.toString()).append('"');
beforeData.append(LINE_END).append(LINE_END);
beforeData.append(params.getString(key.toString()));
beforeData.append(LINE_END);
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
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");
StringBuilder beforeData = new StringBuilder();
try {
for (Iterator iter = params.keys(); iter.hasNext();) {
Object key = iter.next();
if(!String.valueOf(key).equals("headers"))
{
beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
beforeData.append("Content-Disposition: form-data; name=\"").append(key.toString()).append('"');
beforeData.append(LINE_END).append(LINE_END);
beforeData.append(params.getString(key.toString()));
beforeData.append(LINE_END);
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
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");
JesseMonroy650 (Volunteer), Champion
- 3325 Posts
- 122 Reply Likes
JesseMonroy650 (Volunteer), Champion
- 3325 Posts
- 122 Reply Likes
@Otto,
Okay. if you are look at the Java code, your are doing the wrong thing. Unless you plan on rewriting the plugin, don't waste your time on this.
Please restate what you are trying to do, because your original message is convoluted.
Jesse
Okay. if you are look at the Java code, your are doing the wrong thing. Unless you plan on rewriting the plugin, don't waste your time on this.
Please restate what you are trying to do, because your original message is convoluted.
Jesse
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
I don't think you are reading the java code correctly. The 'name' is not assigned twice.
Rather, it's an iteration of key/value pairs followed by some one-time post data for the file (name, size, mime type, etc). The string "name=" is NOT the filename, but rather just a string contained in the post headers, following the string 'Content-Disposition: form-data;'
The result would be something like
Rather, it's an iteration of key/value pairs followed by some one-time post data for the file (name, size, mime type, etc). The string "name=" is NOT the filename, but rather just a string contained in the post headers, following the string 'Content-Disposition: form-data;'
The result would be something like
Content-Type: multipart/form-data; boundary=---[...]754266
Content-Length: 521
---[...]754266
Content-Disposition: form-data; name="key1"
text value1
---[...]754266
Content-Disposition: form-data; name="key2"
text value2
---[...]754266
Content-Disposition: form-data; name="file1"; filename="img.png"
Content-Type: image/png
contents of img.png
---[...]754266--- 4 Posts
- 0 Reply Likes
Thanks for getting back to me you guys, sorry for my poor clarity. What I was trying to convey was that the "name" string as outputted by the server I was using contained only one name, not an iteration of the key:value pairs and then the actual name, even with key value pairs added. In other words, it only listed this part:
Upon further inspection, I think it may be an issue with the way the server was handling it. The workaround I'm using gets the job done, but I figured I'd try to clarify what was causing the difference between my result and the result example Petra posted.
Content-Disposition: form-data; name="file1"; filename="img.png"
Content-Type: image/png
contents of img.png
Upon further inspection, I think it may be an issue with the way the server was handling it. The workaround I'm using gets the job done, but I figured I'd try to clarify what was causing the difference between my result and the result example Petra posted.
JesseMonroy650 (Volunteer), Champion
- 3325 Posts
- 122 Reply Likes
@Otto,
In this forum (and most as far as i know about phonegap), issues with Servers are not addressed - except in the simplest cases where are looking to validate connectivity.
Best of Luck
Jesse
In this forum (and most as far as i know about phonegap), issues with Servers are not addressed - except in the simplest cases where are looking to validate connectivity.
Best of Luck
Jesse
Related Categories
-
Plugins
- 1283 Conversations
- 38 Followers
-
Programming (Others)
- 1167 Conversations
- 23 Followers



