通过List< HttpPostedFileBase>发布多个文件 [英] Post multiple files by List<HttpPostedFileBase>

查看:81
本文介绍了通过List< HttpPostedFileBase>发布多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想通过模型将多个文件发布到控制器.

I just want to post my multiple files to controller via model.

class myModel{
    public List<string> contexts {get;set;}
    public List<HttpPostedFileBase> images {get;set;}
}

还有Html

<input id="myImages" type=file multiple="multiple>

用户将文件添加到输入标记和文本输入中,这些文件根据文件数动态创建

User add files to input tag and text inputs creating dynamically based on files count

<input id="text0"></input>
<input id="text1"></input>
<input id="text3"></input>
           ...
           .

用户填写文本输入后.点击保存按钮. 我想通过我的模型将这些数据传递给我的控制器. 我尝试使用表单数据,但无法成功

After user fill the text inputs. hit the save button. and I want to pass these datas to my controller via my model. I try form data but I couldn't be success

有我的尝试

<script>
    function someFunction(){
        var data = new FormData();
        for (i = 0; i < $("#myImages")[0].files.length; i++) {
            data.append("images[]", $("#myImages")[0].files[i]);
            data.append("contexts[]", $("#text" + i).val());
        }
        $.ajax
        ({
            url: "/Admin/CreateGallery",
            type: "POST",
            data: data,
            contentType: false,
            processData: false,
            success:function(){
                alert("very well");
            }
        });
    }
</script>

我可以获取上下文,但是图像列表始终为空

I can get contexts but images list always null

推荐答案

只需更改此内容即可.

data.append("images[]", $("#myImages")[0].files[i]);

对此

data.append("images", $("#myImages")[0].files[i]);

这篇关于通过List&lt; HttpPostedFileBase&gt;发布多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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