在输入名称和数量是动态的情况下,如何处理多个文件上传? [英] How to handle multiple file uploads where the name and number of inputs is dynamic?

查看:94
本文介绍了在输入名称和数量是动态的情况下,如何处理多个文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过文件输入上传多个图像,其中name和输入数量是动态的.

I am uploading multiple images through file inputs where the name and number of inputs is dynamic.

但是它们确实遵循此命名约定:

They do follow this naming convention however:

<input name = "image_path" ...
<input name = "image_path_F1" ...
<input name = "image_path_F2" ...
<input name = "image_path_F3" ...

输入作为 FormData 对象发送.

在处理我以前使用过的Python脚本中的单个图像时:

When handling a single image from the Python scripts I have previously used:

uploaded_image = request.files.name_of_file_input_here

问题

request.files是否有通用的全部捕获"类型的方法,可以像这样使用:

Is there a generic 'catch all' type of method available with request.files which could be used like:

uploaded_images = request.files.*all

或者我是否需要创建某种循环来处理可能的文件名,例如:

Or will I need to create some sort of loop to handle the possible file names eg:

客户端(jQuery)

var names_array = ["image_path","image_path_F1","image_path_F2"];
var length_of_names_array = 3;

已发送给Python ...

sent to Python...

Python

names_array = request.forms.names_array
length_of_names_array = request.forms.length_of_names_array

counter = 1
for i, val in enumerate(range(length_of_names_array)):
    if i == 0:
        constructor = "image_path"
        request.files.constructor
    else:
        constructor = "image_path_F" + str(counter)
        request.files.constructor
        counter += 1

上面的代码只会生成正确的名称(实际上,我不确定上面的request.files方法是否可以与constructor- edit 一起使用,但似乎没有) .

The above code will just generate the correct names (and actually I'm not sure if the request.files method above would work with constructor - edit, it doesn't seem to).

在这里的方法中可能找到一种解决方案:

There may be a solution to be found in the approach here:

https://stackoverflow.com/a/3111795/1063287

但是我不太了解它的工作方式或将其应用于上述情况的确切方式:

But I don't quite understand how it operates or exactly how it could be applied to the above scenario:

如果您不知道密钥,则可以遍历文件:

If you don't know the key, you can iterate over the files:

for filename, file in request.FILES.iteritems():
    name = request.FILES[filename].name

推荐答案

这似乎有些晦涩,因为我不太确定它是如何工作的,但是它可以工作.

This seems a bit dodgy cause I'm not quite sure how it works yet, but it is working.

这是Mongo的实现,但是我在这里添加它是因为我想知道的是,您可以定义和访问多个文件输入,其中name是动态实体,而name是动态实体输入.

It's a Mongo implementation, however I am adding it here because I think the takeaway is that you can define and access multiple file inputs where the name is a dynamic entity as are the number of inputs.

使用iteritems()启用了此功能的原因.

What enabled this was using iteritems().

Python

for key, value in request.files.iteritems():
    uploaded_image = request.files[key]
    name, ext = os.path.splitext(uploaded_image.filename)
    if ext not in ('.png','.jpg','.jpeg','.JPG','.gif'):
        return "File extension not allowed."
    if uploaded_image and uploaded_image.file:
        raw = uploaded_image.file.read()
        filename = uploaded_image.filename
        dbname = 'grid_files'
        db = connection[dbname]
        fs = gridfs.GridFS(db)
        fs.put(raw,filename=filename, user_email = user_email)

这会将每个图像与文件名和用户电子邮件一起添加到数据库中,以进行唯一标识(以及MongoDB自动生成的时间和长度字段).

This adds each image to the database with a filename and a user email for unique identification (as well as time and length fields automatically generated by MongoDB).

在客户端,输入被附加到 FormData 对象:

On the client side, the inputs were appended to the FormData object with:

jQuery

var image_inputs = $("#my_form input[type=file]");
$.each(image_inputs,function(obj,v) { 
var file = v.files[0];
var filename = $(v).attr("data-img_filename");
var name = $(v).attr("id");
myFormData.append(name, file, filename);
});

似乎文件名会自动包含在内,而无需按上述定义:

And it seems the filename is automatically included without needing to define it as above:

https://stackoverflow.com/a/21942376/1063287

这篇关于在输入名称和数量是动态的情况下,如何处理多个文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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