setu
setu

Reputation: 181

Multiple File Upload in asp.net mvc

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>`

Upvotes: 3

Views: 8308

Answers (2)

kogelnikp
kogelnikp

Reputation: 447

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)
{
    ...
}

Upvotes: 3

mxmissile
mxmissile

Reputation: 11673

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

Upvotes: 6

Related Questions