I need to read data from FormData? I try to read something like someFormatData["valueName"]
but it not working.
options["fileId"]
or options["file"]
does not work. Also I try options.fileId
same result:
JavaScript
x
44
44
1
function upload(file, fileId, callback) {
2
var formData = new FormData();
3
formData.append("file", file);
4
formData.append("fileID", fileId);
5
6
$.ajax({
7
url: '/url',
8
type: 'POST',
9
data: formData,
10
processData: false,
11
contentType: false,
12
success: function(response) {
13
callback(response);
14
}
15
});
16
}
17
18
19
asyncTest("test upload chunk", function() {
20
var blob = new Blob(["Hello world!"], { type: "text/plain" }),
21
options = null,
22
fileID ="someFileID",
23
response;
24
25
jQuery.ajax = function(param) {
26
options = param; // THIS is FormData object
27
// how to read fileId and file from here
28
};
29
30
upload(blob, fileID, function (data) {
31
response = data;
32
});
33
34
options.success({
35
someProp: 'responseFromServer'
36
});
37
38
setTimeout(function() {
39
QUnit.equal(options, "dataTosend", "parameters is OK");
40
QUnit.equal(response["someProp"], "responseFromServer", "Response ok");
41
start();
42
},1000);
43
});
44
Advertisement
Answer
If you take your FormData
object you can use a few different methods on it… What you are looking for is
JavaScript
1
2
1
formData.get()
2
or
JavaScript
1
2
1
formData.getAll()
2
https://developer.mozilla.org/en-US/docs/Web/API/FormData
Note that the get()
method is not fully supported on all browsers.