3

I need to get files from a single file uploader and a multiple file uploader from the same form. And also need to know from which input field those files are coming. From Request.Files i can get all files but can't know from which field those file are coming.

I have a form.

<form> 
    <input type="file" name="file1">
    <input type="file" name="files" multiple="true"> 
</form>`
setu
  • 181
  • 1
  • 2
  • 13
  • from the original asp.net mvc author himself... http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/ – mxmissile Mar 24 '16 at 21:41
  • Thanks for response but that is not what i am searching for. i have edited my question. – setu Mar 24 '16 at 21:57

2 Answers2

6

Use a model instead of Request.Files directly. Based off your view you could do something like this:

public class UploadForm
{
    public HttpPostedFileBase file1 {get;set;}

    public IEnumerable<HttpPostedFileBase> files {get;set;}
}

And then in your action:

public ActionResult Uploade(UploadForm form)
{
    if(form.file1 != null)
    {
        //handle file
    }

    foreach(var file in form.files)
    {
        if(file != null)
        {
            //handle file
        }
    }
    ...
}
mxmissile
  • 11,464
  • 3
  • 53
  • 79
3

If these two upload controls have different name attributes you can let the model binder do the work. You just have to name the parameter in the controller action the same as the name of your upload control.

public ActionResult Upload(HttpPostedFileBase file1, IEnumerable<HttpPostedFileBase> files)
{
    ...
}
kogelnikp
  • 447
  • 7
  • 11