Skip to content

Commit fa06f5e

Browse files
committed
fix sending images and strings with FormData
1 parent f04085b commit fa06f5e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/core/client.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ DL.Client.prototype.getPayload = function(method, data) {
253253
payload = data;
254254
} else if (method !== "GET") {
255255
var field, value, filename,
256-
formdata = new FormData(),
257-
worth = false;
256+
formdata = new FormData(),
257+
worth = false;
258258

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

266-
} else if (typeof(value)==="string") {
267-
//
268-
// Do nothing...
269-
//
270-
// IE8 can't compare instanceof String with HTMLInputElement. LOL
266+
} else if (typeof(value)==='boolean' || typeof(value)==='number' || typeof(value)==="string") {
267+
value = value.toString();
268+
269+
// IE8 can't compare instanceof String with HTMLInputElement.
271270
} else if (value instanceof HTMLInputElement && value.files && value.files.length > 0) {
272271
filename = value.files[0].name;
273272
value = value.files[0];
@@ -290,24 +289,35 @@ DL.Client.prototype.getPayload = function(method, data) {
290289
// Consider serialization to keep data types here: http://phpjs.org/functions/serialize/
291290
//
292291
if (!(value instanceof Array)) { // fixme
293-
try {
294-
formdata.append(field, value, filename || "file");
295-
} catch (e) {
292+
if (typeof(value)==="string") {
293+
formdata.append(field, value);
294+
} else {
296295
try {
297-
// on cli-console (nodejs), here throwns error when using Collection.updateAll
298-
formdata.append(field, value);
299-
} catch (e2) {}
296+
formdata.append(field, value, filename || "file");
297+
} catch (e) {
298+
// TODO:
299+
// Node.js (CLI console) throws exception here
300+
}
300301
}
301302
}
302-
303303
}
304304

305305
if (worth) {
306306
payload = formdata;
307307
}
308308
}
309309

310-
payload = payload || JSON.stringify(data);
310+
payload = payload || JSON.stringify(data, function(key, value) {
311+
if (this[key] instanceof Date) {
312+
return Math.round(this[key].getTime() / 1000);
313+
} else {
314+
return value;
315+
}
316+
});
317+
318+
// empty payload, return null.
319+
if (payload == "{}") { return null; }
320+
311321
if (method==="GET" && typeof(payload)==="string") {
312322
payload = encodeURIComponent(payload);
313323
}

0 commit comments

Comments
 (0)