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);
});
2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @MarcinNabialek been a long day and forgot the markup!
get() does not, getAll() is supported by Chrome developer.mozilla.org/en-US/docs/Web/API/FormData/…
Yes, I saw it on that page, but both methods aren't work in my Chrome (v40)
see at developer.mozilla.org/en-US/docs/Web/API/FormData/get. it says that chrome, IE and safari dont support those methods.
0

You can read using this

formData.get('fileId') // to read Id
formData.get('file') // to read the file 

Comments

0

Another way to list all entries of a FormData :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.