简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM