7

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:

function upload(file, fileId, callback) {
    var formData = new FormData();
    formData.append("file", file);
    formData.append("fileID", fileId);

    $.ajax({
        url: '/url',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            callback(response);
        }
    });
}


asyncTest("test upload chunk", function() {
    var blob = new Blob(["Hello world!"], { type: "text/plain" }),        
        options = null,
        fileID ="someFileID",
        response;

    jQuery.ajax = function(param) {
        options = param;   // THIS is FormData object 
        // how to read fileId and file from here
    };

    upload(blob, fileID, function (data) {
        response = data;  
    });

    options.success({
        someProp: 'responseFromServer'
    });

    setTimeout(function() {
        QUnit.equal(options, "dataTosend", "parameters is OK");
        QUnit.equal(response["someProp"], "responseFromServer", "Response ok");
        start();
    },1000);
});
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Taras Kravets
  • 1,443
  • 4
  • 14
  • 15
  • 2
    http://stackoverflow.com/questions/7752188/formdata-appendkey-value-is-not-working – BobTheBuilder Feb 14 '13 at 14:28
  • Is param the FormData object, or param.data ? From the doc at https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/FormData, I don't think there is a way to access the data. However, unless you're testing $.ajax, you might want to spy on FormData to check that you called append with the right ids... – phtrivier Feb 14 '13 at 14:30

3 Answers3

6

If you take your FormData object you can use a few different methods on it… What you are looking for is

formData.get()

or

formData.getAll()

https://developer.mozilla.org/en-US/docs/Web/API/FormData

Note that the get() method is not fully supported on all browsers.

Mike
  • 14,010
  • 29
  • 101
  • 161
Kris Boyd
  • 798
  • 10
  • 17
0

You can read using this

formData.get('fileId') // to read Id
formData.get('file') // to read the file 
Koushik Das
  • 9,678
  • 3
  • 51
  • 50
0

Another way to list all entries of a FormData :

for(const entry of formData){
  console.log(entry); // Array: ['entryName', 'entryValue']
}
Daweb
  • 64
  • 5