File-transfer params overwriting name in file-transfer upload options

  • 1
  • Problem
  • Updated 4 years ago
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.
Photo of Otto Metrics

Otto Metrics

  • 4 Posts
  • 0 Reply Likes

Posted 4 years ago

  • 1
Photo of Otto Metrics

Otto Metrics

  • 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");
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
When posting code, please use some HTML (like <code></code>). (see attached image)

Photo of JesseMonroy650 (Volunteer)

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
Photo of Petra V.

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

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--
Photo of Otto Metrics

Otto Metrics

  • 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:

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.
Photo of JesseMonroy650 (Volunteer)

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