1

I want to add the ability to upload an image and save it in my database. I have a table and one of its columns is an Image data type. I followed this link, and some similar links, but it doesn't seem to work. Here is the code I tried:

if (Request.Files.Count > 0 && Request.Files[0] != null)
{
    HttpPostedFileBase file = Request.Files[0];
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), file.FileName);
    file.SaveAs(path);
}

But the file doesn't save in the specified folder.

Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
starrr
  • 1,013
  • 1
  • 17
  • 48
  • try debugging. what path string path variable contains? Is Request.Files empty? Please provide some more details. – ILya May 31 '12 at 07:01
  • Do you receive an exception? Why save the file in a folder when you want to save in a database table? – Ralf de Kleine May 31 '12 at 07:02
  • 1
    check this thread http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc3-0/5193851#5193851 – VJAI May 31 '12 at 07:35

1 Answers1

3

You have to make sure the encType is set to multipart/form-data in the HTML form.

Ex.

@using (Html.BeginForm("Index", "Home", FormMethod.Post, 
      new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}
VJAI
  • 32,167
  • 23
  • 102
  • 164