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

Asked

Viewed 197 times

3

The following code inserts files from a field file, in 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

It is possible to remove a file/field previously added to FormData, or 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 occurs (event onchange) in a field input of the kind file. At the end, formData will be sent by AJAX.

1 answer

2


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

  • Yes, I had already started making modifications to work this way. Soon put whether it worked or not.

  • Yes, after a lot of effort with Javascript and a few hours of debugging, I was able to make it work. I added drag & drop support as well.

  • Glad you decided! @Calebeoliveira

Browser other questions tagged

You are not signed in. Login or sign up in order to post.