How to remove a field already added to a 'FormData'?

3

The following code inserts files from a file field, into a FormData :

var filedata = event.target; // FileList object
var i = 0, len = filedata.files.length, file;

for (; i < len; i++) {
    file = filedata.files[i];
    // ...

    // Aqui adiciono o campo ao FormData
    formData.append('img_upload_'+(countFiles++), file);
}

Question

Is it possible to remove a file / field previously added to FormData , or do I have to create a new instance, and have the user add all the files again?

The above method is executed every time a modification happens (event onchange ) in a input field of type file . At the end, formData will be sent by AJAX.

    
asked by anonymous 05.01.2014 / 00:29

1 answer

2

The FormData only has method append . However, in this scenario it would be better not to populate it with onchange . If necessary, you can maintain an array with File objects, and the array can be manipulated when needed. Leave it to create / pop FormData only at the end just before the AJAX request.

    
05.01.2014 / 02:17