Skip to content

Commit

Permalink
fix sending images and strings with FormData
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Nov 24, 2014
1 parent f04085b commit fa06f5e
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ DL.Client.prototype.getPayload = function(method, data) {
payload = data;
} else if (method !== "GET") {
var field, value, filename,
formdata = new FormData(),
worth = false;
formdata = new FormData(),
worth = false;

for (field in data) {
value = data[field];
Expand All @@ -263,11 +263,10 @@ DL.Client.prototype.getPayload = function(method, data) {
if (typeof(value)==='undefined' || value === null) {
continue;

} else if (typeof(value)==="string") {
//
// Do nothing...
//
// IE8 can't compare instanceof String with HTMLInputElement. LOL
} else if (typeof(value)==='boolean' || typeof(value)==='number' || typeof(value)==="string") {
value = value.toString();

// IE8 can't compare instanceof String with HTMLInputElement.
} else if (value instanceof HTMLInputElement && value.files && value.files.length > 0) {
filename = value.files[0].name;
value = value.files[0];
Expand All @@ -290,24 +289,35 @@ DL.Client.prototype.getPayload = function(method, data) {
// Consider serialization to keep data types here: http://phpjs.org/functions/serialize/
//
if (!(value instanceof Array)) { // fixme
try {
formdata.append(field, value, filename || "file");
} catch (e) {
if (typeof(value)==="string") {
formdata.append(field, value);
} else {
try {
// on cli-console (nodejs), here throwns error when using Collection.updateAll
formdata.append(field, value);
} catch (e2) {}
formdata.append(field, value, filename || "file");
} catch (e) {
// TODO:
// Node.js (CLI console) throws exception here
}
}
}

}

if (worth) {
payload = formdata;
}
}

payload = payload || JSON.stringify(data);
payload = payload || JSON.stringify(data, function(key, value) {
if (this[key] instanceof Date) {
return Math.round(this[key].getTime() / 1000);
} else {
return value;
}
});

// empty payload, return null.
if (payload == "{}") { return null; }

if (method==="GET" && typeof(payload)==="string") {
payload = encodeURIComponent(payload);
}
Expand Down

0 comments on commit fa06f5e

Please sign in to comment.