Skip to content

Commit ea5cf26

Browse files
committed
Fixes to better handle binary FormData.
1 parent e839e69 commit ea5cf26

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

src/www/ios/formdata-polyfill.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@
241241
for (var i = 0; i < entries.length; i++)
242242
{
243243
var entry = entries[i];
244-
bodyEntries.push(__FormData._generateMultipartFormData(entry, boundary));
244+
for (var data of __FormData._generateMultipartFormData(entry, boundary))
245+
bodyEntries.push(data);
245246
}
246247

247248
bodyEntries.push("--" + boundary + "--");
248-
parts.body = bodyEntries.join("");
249+
parts.body = new Blob(bodyEntries, {type: "application/octet-stream"});
249250

250251
return parts;
251252
};
@@ -305,11 +306,11 @@
305306
resolve({name: name, value: reader.error, filename: filename, type: type});
306307
};
307308

308-
reader.readAsBinaryString(value);
309+
reader.readAsArrayBuffer(value);
309310
}
310311
else
311312
{
312-
promise = resolve({name: name, value: "" + value});
313+
promise = resolve({name: name, value: value});
313314
}
314315
});
315316

@@ -318,24 +319,31 @@
318319

319320
__FormData._generateMultipartFormData = function (entry, boundary)
320321
{
321-
var data = "";
322+
var data = [];
322323

323-
data += "--" + boundary + "\r\n";
324+
data.push("--" + boundary + "\r\n");
324325
if (!entry['filename'])
325326
{
326-
data += 'content-disposition: form-data; name="' + entry.name + '"\r\n';
327-
data += '\r\n';
328-
data += entry.value + "\r\n";
327+
data.push('content-disposition: form-data; name="');
328+
data.push(entry.name);
329+
data.push('"\r\n');
330+
data.push('\r\n');
331+
data.push(entry.value);
332+
data.push("\r\n");
329333
}
330334
else
331335
{
332336
// Describe it as form data
333-
data += 'content-disposition: form-data; ';
334-
data += 'name="' + entry.name + '"; ';
335-
data += 'filename="' + entry.filename + '"\r\n';
336-
data += 'Content-Type: ' + entry.type + '\r\n';
337-
data += '\r\n';
338-
data += entry.value + '\r\n';
337+
data.push('content-disposition: form-data; name="');
338+
data.push(entry.name)
339+
data.push('"; filename="');
340+
data.push(entry.filename);
341+
data.push('"\r\n');
342+
data.push('Content-Type: ');
343+
data.push(entry.type);
344+
data.push('\r\n\r\n');
345+
data.push(entry.value);
346+
data.push('\r\n');
339347
}
340348

341349
return data;

src/www/ios/xhr-polyfill.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,19 @@
327327
if (!contentType)
328328
reqContext.requestHeaders["content-type"] = parts.contentType;
329329

330-
resolve(HttpHandler._convertTextToBinaryBase64String(contentType, parts.body));
330+
var reader = new FileReader();
331+
reader.onload = function ()
332+
{
333+
var base64str = btoa(reader.result);
334+
resolve(base64str);
335+
};
336+
reader.onerror = function ()
337+
{
338+
var base64str = btoa(reader.error);
339+
reject(base64str);
340+
};
341+
342+
reader.readAsBinaryString(parts.body);
331343
});
332344
});
333345

tests/tests.js

+53
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,59 @@ exports.defineAutoTests = function ()
247247

248248
});
249249

250+
it("download/upload/binary compare", function(done)
251+
{
252+
// 1) download an image as a blob
253+
// 2) add the blob to a form data and send it back to the server as a multipart form
254+
// 3) server performs a binary compare of the original file to the uploaded
255+
// 4) returns a document response with the results of the compare
256+
257+
var downloadPromise = new Promise(function (resolve, reject)
258+
{
259+
var xhr = new XMLHttpRequest();
260+
xhr.onloadend = function (event)
261+
{
262+
resolve(xhr.response);
263+
};
264+
xhr.onerror = function (event)
265+
{
266+
reject(xhr.response);
267+
268+
};
269+
270+
xhr.responseType = "blob";
271+
xhr.open("GET", SECURE_TESTS_DOMAIN + "/RestApp-ViewController-context-root/servletmultipartplayback");
272+
xhr.send();
273+
});
274+
275+
downloadPromise.then(function (blob)
276+
{
277+
expect(blob).toBeDefined();
278+
expect(blob instanceof Blob).toEqual(true);
279+
expect(blob.type).toEqual("image/png");
280+
281+
var fd = new FormData();
282+
fd.append("simple-test", blob, "simple-test.html");
283+
284+
var xhr = new XMLHttpRequest();
285+
xhr.onloadend = function ()
286+
{
287+
expect(this.readyState).toBe(this.DONE);
288+
expect(this.status).toBe(200);
289+
expect(this.response instanceof Document).toEqual(true);
290+
expect(this.response.querySelector("#are-same").textContent).toEqual("true");
291+
done();
292+
};
293+
xhr.responseType = "document";
294+
xhr.open("POST", SECURE_TESTS_DOMAIN + "/RestApp-ViewController-context-root/servletmultipartplayback");
295+
xhr.send(fd);
296+
297+
});
298+
299+
300+
}, 120000);
301+
302+
250303
});
251304

252305
describe('http:// GET remote:', function ()

0 commit comments

Comments
 (0)