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.

3
  • try debugging. what path string path variable contains? Is Request.Files empty? Please provide some more details. Commented May 31, 2012 at 7:01
  • Do you receive an exception? Why save the file in a folder when you want to save in a database table? Commented May 31, 2012 at 7:02
  • 1
    check this thread stackoverflow.com/questions/5193842/file-upload-asp-net-mvc3-0/… Commented May 31, 2012 at 7:35

1 Answer 1

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" />
}
Sign up to request clarification or add additional context in comments.

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.