0

I have an MVC application and this is what I intend to do. I have a field that is supposed to store the path to the file that I upload onto the server. This is the model:

 int ID
 string Title
string Path

And here is my controller:

    public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
               db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

        ViewBag.CreatedBy = new SelectList(db.Users, "ID", "UserName", book.CreatedBy);
        return View(book);
    }

I cant find a control to upload files in MVC 3. I dont know if there is a workaround since all solutions available treat the file as a separate entity on its own.

Any sample code will be appreciated.

2
  • Perhaps this article can help codeproject.com/Articles/38970/… Or stackoverflow.com/questions/12430446/… Commented Jan 6, 2013 at 16:38
  • yeah, I have seen the first link. But like I said, it treats the file as the only entity. my book entity has three properties and so if I use this approach, I wont be able to retrieve them. Remember my ActionResult method is also expecting a Book return value Commented Jan 6, 2013 at 16:49

1 Answer 1

2

It's possible to POST both an uploaded file, as well as other form variables. First, declare your controller:

public ActionResult Create(Book book, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        db.Books.Add(book);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.CreatedBy = new SelectList(db.Users, "ID", "UserName", book.CreatedBy);
    return View(book);
}

and then in your form:

@using (Html.BeginForm("Create", "Book", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
    @Html.TextBoxFor(m => m.Title)
    <input type="file" id="file" name="file" />
    <input type="submit" value="Upload" />
}

This will POST both a file and "Title". You can then do what you want with the file, accessing it's filename in file.FileName, or saving it somewhere. The key is setting the forms enctype to "multipart/form-data". See this tutorial for other examples

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for you response. I tried this and now am not getting any exceptions. But for some strange reason, all the browsers report that the connection has been terminated but all other pages work fine. Pardon me for am a student. Do you know any reason why this is so?
What's the full error that you're getting? Does the browse developer console report anything? If you set a breakpoint in Create(), do you see the values correctly set (i.e. not null)?

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.