Friday, November 12, 2010

Solving Request.Files.Count is 0 in mvc

Solving problem while uploading files in ASP.NET MVC 2

You should set both name and id attributes for fileupload control.

You should set enctype attribute of form element into multipart/form-data

Try your self uploading file.

See the following Example for more details

<form method="post" enctype="multipart/form-data" action="MyController/Upload">
    <input type="file" class="file" id="File1" name="File1" />
    <input type="submit" id="Submit1" class="btn80" value="Upload" />
</form>



public ActionResult Upload()
{
    foreach (string upload in Request.Files)
    {
        var file = Request.Files[upload];
        string filePath = "C:\\Temp\\" + DateTime.Now.Ticks.ToString() + file.FileName;
        file.SaveAs(filePath);
                  
    }
    return View();
}