Post multiple files by List<HttpPostedFileBase>

谁说胖子不能爱 提交于 2019-12-11 03:45:08

问题


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;}
}

And there is 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

there is my trying

<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


回答1:


Just changing this.

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

to this

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


来源:https://stackoverflow.com/questions/39425184/post-multiple-files-by-listhttppostedfilebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!