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>`
2
  • from the original asp.net mvc author himself... haacked.com/archive/2010/07/16/…
    – mxmissile
    Mar 24, 2016 at 21:41
  • Thanks for response but that is not what i am searching for. i have edited my question.
    – setu
    Mar 24, 2016 at 21:57

2 Answers 2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.