0

Here is my code -

//in Profile.cshtml
@{
    ViewBag.Title = "Profile";
}

<h2>Profile</h2>

@using (Html.BeginForm("Upload", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

In my controller (ProfileController.cs) I have -

namespace MvcApplication1.Controllers
{
    public class ProfileController : Controller
    {

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Profile() {
        return View();
    }

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file) {
        if (file.ContentLength > 0) {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Media/uploads"), fileName);
            file.SaveAs(path);
        }
        return RedirectToAction("Profile");
    }

}

}

but everytime I click on "OK" to upload the file, I get this error - localhost:12345/Profile/Upload - Internet explorer can not display this webpage.

I have a method called Upload in my Profile controller that should accept this file.

What am I missing?

5
  • 1
    Why is your HttpPost attribute commented out on your method? Looks like you don't have an action method to handle the Post verb, thus the 404 Commented Dec 9, 2013 at 20:47
  • Bad copy and paste job. This still doesn't work with that line not commented out Commented Dec 9, 2013 at 20:57
  • Did you put a breakpoint in to confirm that it never hits that controller action? I'm just wondering if the problem is the redirect because there's no profile view and not the file upload action. Commented Dec 9, 2013 at 21:10
  • 1
    I agree with BoredBlazer, have you tried just to just return the View you want instead of Redirecting to it (ex: return View("Profile");)? Commented Dec 9, 2013 at 21:14
  • Yes, I added a breakpoint that never gets hit. Commented Dec 9, 2013 at 21:34

1 Answer 1

1

It does not appear that you have an action method to handle the POST verb request. The default verb (if not specified) on an action method is GET. While you do have an action method in your code sample, the [HttpPost] attribute is commented out. This will make that method only available to GET requests -- you will get a 404 (Page cannot me displayed) otherwise.

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

2 Comments

Bad copy and paste job. This still doesn't work with that line not commented out.
There is no reason syntactically that this should not work. I would set a breakpoint on the action method and make sure it is being hit. It is possible that perhaps a firewall or an HttpModule is grabbing the request first and blocking it -- have you checked for this? For example, you can configure UrlScan to block specific Http Verbs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.