0

I use a custom file uploader and need to pass file parameter with the name of files[]. Because it does not make any sense and the file data is sent by this default name of the file uploader. However, I cannot use the same parameter name in the Controller. So, how can I use files[] as the name parameter of input control?

View:

<input type="file" name="files[]" id="filer_input" multiple="multiple" >


<script>    

    function create(event) {

        event.preventDefault();
        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
        });
    };

</script>


Controller:

public JsonResult Insert([Bind(Exclude = null)] ViewModel model, 
    IEnumerable<HttpPostedFileBase> files)
{
    //code removed for brevity
}

Any idea?

Jack
  • 1
  • 21
  • 118
  • 236
  • 2
    If everything else works as expected you should find the files in `Request.Files`, so you don't actually need to bind them to a method parameter. – Cristi Pufu Oct 07 '16 at 17:01
  • 2
    Not sure but you may give it a try using var files=Request["files[]"] – Mir Gulam Sarwar Oct 07 '16 at 17:01
  • 2
    The default ModelBinder doesn't work for multipart requests. You need to use `Request.Files` as mentioned above - or create/find another ModelBinder – Rory McCrossan Oct 07 '16 at 17:03
  • 1
    http://stackoverflow.com/questions/15680629/mvc-4-razor-file-upload/15680783#15680783 – Cristi Pufu Oct 07 '16 at 17:07
  • @CristiPufu **var file = Request.Files[0];** solved the problem. Many thanks for your help... – Jack Oct 07 '16 at 17:11
  • @CristiPufu Can I also post file data by creating a new parameter as **public ICollection FilesInput { get; set; }** in the ViewModel? If so, which one is better? Request.Files or ViewModel approach? – Jack Oct 07 '16 at 17:31
  • 1
    All you need to do is change the input to `` (and the `Default ModelBinder` **does** work for multipart requests). And always use a view model so you have the opportunity to add validation. –  Oct 07 '16 at 20:50
  • @StephenMuecke Hmm, yes you are right ViewModel is more advantageous. But I cannot apply it to multiple fiile upload. Could you please have a look at Multiple File Uploads in ASP.NET MVC using View Models? Many thanks for your help... – Jack Oct 08 '16 at 08:02
  • For anyone else who encountered such a kind of problem might look at the post on [Multiple File Uploads in ASP.NET MVC using View Models](http://stackoverflow.com/questions/39930119/multiple-file-uploads-in-asp-net-mvc-using-view-models/39931778#39931778). FYI... – Jack Oct 08 '16 at 16:44

2 Answers2

0

Maybe, something like that could help you.

<input type="file" name="filesInput" id="filer_input" multiple="multiple" >

public JsonResult Insert(FormCollection formCollection)
{
    ...formCollection["filesInput"]
}

You can try to add a collection "FilesInput" on ViewModel and let the modelbinder do the work for you. In this way you'll have only model param for Insert method.

  • It is really good idea that I thought before. But after trying it I see that the FilesInput parameter posted as FilesInput[] ("[]" is automatically added due to upload control maybe) and FilesInput parameter is null in the Controller as files parameter. On the other hand, I added **public byte[] FilesInput { get; set; }** and I think there is no problem regarding to its definition. Is not it? – Jack Oct 07 '16 at 17:19
  • I also tried to use **public ICollection FilesInput { get; set; }** in the ViewModel but unfortunately it does not make any sense and still null value pass to the Controller. Thanks... – Jack Oct 07 '16 at 17:30
0

refer below link Implement the fileContent functionality in the controller http://www.railscook.com/recipes/rails-view-controller-ajax-response-example/