In this article I will explain with an example, how to fetch File from Form Collection in ASP.Net MVC Razor.
The uploaded Files are available in the Request.Files Form Collection in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Namespaces
You will need to import the following namespace.
using System.IO;
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method handles the operation of uploading of Files. The File is fetched from the Request.Files Form Collection using the name of the HTML FileUpload element.
Then a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created and then the file is saved to the Directory (Folder).
Finally, a message is displayed to the user using TempData and a redirection is done to the Index Action method.
Note: For more details on displaying message using TempData, please refer my article ASP.Net MVC: TempData Tutorial with example.
 
public class HomeController : Controller
{
    // GET: Home
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Upload()
    {
        if (Request.Files.Count > 0)
        {
            HttpPostedFileBase postedFile = Request.Files["postedFile"];
            string path = Server.MapPath("~/Uploads/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
 
            postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
            TempData["Message"] = "File uploaded successfully.";
        }
 
        return RedirectToAction("Index");
    }
}
 
 
View
The following View consists of an HTML FileUpload element, a Submit Button and a SPAN element for displaying Message enclosed in a Form element.
The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.
ActionName – Name of the Action. In this case the name is Upload.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
@{
    Layout = null;
}
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <span>Select File:</span>
            <input type="file" name="postedFile" />
            <hr/>
            <input type="submit" value="Upload" />
            <br/>
            <span style="color:green">@TempData["Message"]</span>
        }
    </div>
</body>
</html>
 
 
Screenshot
Fetch File from Form Collection in ASP.Net MVC
 
 
Downloads