I'm trying to use input file with multiple
attribute and to have the formData split so I would have a formData isnatce for each file in the multiple file element.
HTML
<input type="file" name="file" multiple></label>
JS
new FormData(formElement) // <--- Need to have this for each file selected
I need this because I'm using Cloudinary API and they only accept one file at a time, so I need to send multiple Ajax requests and for each, I need formData
instance for each of the files selected, but the problem is, FormData
accepts as an argument the how form
element, and I cannot separate the files.
Each time you make a formData object you can append to it data like this:
but instead of 0 in for loop you can put loop index and send data to ajax.
and you should initialize data by following :
You can use
$.when()
,$.map()
to append eachFile
of<input type="file">
FileList
to newFormData
instance, then call$.ajax()
for eachFormData()
instance.Note, substituted
.on()
foronsubmit
event attribute. Withoutrequired
attribute or a default.value
setname="cloud_name"
andname="upload_preset"
<input type="text">
elements.value
could be an empty string whenform
submit
event occurs.plnkr http://plnkr.co/edit/7l0obsusqXVlommdd49L?p=preview