开发者

File upload problem/bug in MVC 3

I'm having the weirdest problem with file uploads in ASP.NET MVC 3. When I start a new project with the default project template (Internet application) with Razor and add the following to /views/home/index.cshtml

&l开发者_如何转开发t;form action="/Home/Index" method="post" enctype="multipart/form-data">
  <input type="file" name="upfile" />
  <input type="submit" value="post" />
</form>

the upload fails (firebug shows status 'Aborted') whenever I try to upload a file. Some extra info:

  • Windows 7 / 64bit
  • Cassini
  • VS SP1
  • It happens in both Firebug 6.0.2 and IE8

Controller code:

public class HomeController : Controller {
    public ActionResult Index() {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }

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

}

I've been debugging this for a bit and already found out the following:

  • it only happens for files > ~120kb
  • if I add a target Action with attribute [HttpPost] upload succeeds
  • if I debug using fiddler (proxy) upload succeed
  • if I use aspx and add the same code, upload succeeds
  • for aspx/razor, all file are identical, except for (of course) the files in /Views but not /Views/Web.config

Has anyone else experienced this problem, and what is causing it?

update: I know I should use a separate action and mark it with HttpPost, that's not why I'm asking this question. I'm looking for the reason why this doesn't work, not how to solve it.


Without seeing your code, I'm assuming you have a method for HttpGet your default /home/index

You need to post this somewhere, and that wouldn't be your same controller method. You should have a separate method with [HttpPost] on it (separate controller methods for post/get/update/delete)

EDIT for clarification: Create a separate method for your post action. You shouldn't be sharing the same method for get/post. Also you are returning a view from your post. This is also generally not recommended because MVC expects a PRG (post redirect get) behavior so you ideally want to redirect back to an action when done. using Post is also supported here (as well as many other postings on the net)

File upload MVC

In your case above it will work ok as its simple with no validations, but if you have validations on the page before a file upload things can easily get goofy if you post back and don't redirect back to an action.


A separate action to handle the file upload is strongly recommended:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase upfile)
    {
        // TODO: process the uploaded file here
        if (upfile != null && upfile.ContentLength > 0) 
        {
            var fileName = Path.GetFileName(upfile.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
            upfile.SaveAs(path);
        }
        return RedirectToAction("Index");
    }
}

Also make sure you checkout the following blog post.


Have you made sure the MaxRequestStringLength is set correctly in Web.config?

That's usually the problem when I encounter this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜