为< input type = file multiple>中的每个文件创建FormData实例。 [英] create FormData instance for each file in <input type=file multiple>

查看:211
本文介绍了为< input type = file multiple>中的每个文件创建FormData实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有多个属性的输入文件,并且具有formData拆分,因此我将为多个文件元素中的每个文件设置一个formData isnatce。 / p>

HTML



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.

<input type="file" name="file" multiple></label>



JS



JS

new FormData(formElement) // <--- Need to have this for each file selected

我需要这个,因为我使用的是Cloud API,他们一次只接受一个文件,所以我需要发送多个Ajax请求,对于每一个,我需要 formData 实例为每个选定的文件,但问题是, FormData 接受作为参数如何形式元素,我不能分开文件。

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.

推荐答案

每次你制作一个formData对象,你可以像这样附加数据:

Each time you make a formData object you can append to it data like this:

data.append("file", document.getElementById(file).files[0]);

但是在for循环中可以放置循环索引并将数据发送到ajax。

but instead of 0 in for loop you can put loop index and send data to ajax.

,您应该按照以下原则初始化数据:

and you should initialize data by following :

 var data = new FormData();

这篇关于为&lt; input type = file multiple&gt;中的每个文件创建FormData实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆