2

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

ekad
  • 14,436
  • 26
  • 44
  • 46
Ozan Ertürk
  • 465
  • 5
  • 17

1 Answers1

3

Just changing this.

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

to this

data.append("images", $("#myImages")[0].files[i]);
Ozan Ertürk
  • 465
  • 5
  • 17