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/… Commented Mar 24, 2016 at 21:41
  • Thanks for response but that is not what i am searching for. i have edited my question. Commented 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
        }
    }
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.