How to upload multiple files with jQuery.filer plugin in ASP.NET MVC 5 using View Models

前端 未结 1 924
慢半拍i
慢半拍i 2021-01-07 14:35

I am using jQuery.filer on a FileUpload control in an MVC5 project and I want to post multiple files from View to Controller

相关标签:
1条回答
  • 2021-01-07 15:05

    =============================== S O L V E D ================================

    Here is the solution by @StephenMuecke. Many thanks for his huge help...

    View:

    @model ExperimentViewModel
    
    //Change 1
    @Html.TextBoxFor(m => m.FileUpload, new { type = "file", multiple = "multiple" })
    
    <script>        
        function create(event) {
            event.preventDefault();
    
            //Change 2 : Because jquery.filer adds "[]" to the Name parameter!!!
            $('#FileUpload').attr('name', 'FileUpload');
    
            var formdata = new FormData($('#frmCreate').get(0)); 
    
            $.ajax({
                type: "POST",
                url: '@Url.Action("Create", "Experiment")',
                cache: false,
                dataType: "json",
                data: formdata,         
                processData: false, 
                contentType: false
            });
        };
    
        $('#FileUpload').filer({
    
        //code omitted for brevity
    
        //Change 3: Comment out uploadFile section
        //uploadFile: { ... }
    
        });
    <script>
    


    ViewModel:

    public class ExperimentViewModel
    {
        //code omitted for brevity
    
        //Change 4
        [DataType(DataType.Upload)]
        public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
    }
    


    Controller:

    public JsonResult Insert(ExperimentViewModel model) //Change 5
    {
        if (ModelState.IsValid)
        {
            //...   
        }
    }
    
    0 讨论(0)
提交回复
热议问题