开发者

mvc file upload with httpRuntime maxRequestLength

I created mvc project and want to upload the file. I registered in the web.config

<httpRuntime maxRequestLength="2000"/> 
<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="address here"> </ customErrors>, in Index.aspx <% using (Html.BeginForm ("upload", "home", FormMethod.Post, 
    new {enctype = "multipart / form-data"})) {%> 
    <label for="file"> Filename: </ label> 
    <input type="file" name="file" id="file" /> 

    <input type="submit" /> 
<%}%> 

In HomeController.cs

[HttpPost] 
public ActionResult Upload (HttpPostedFileBase file) 
{ 
    if (file! = null & & file.ContentLength> 0) 
    { 
        if (file.ContentLe开发者_JAVA技巧ngth> 4096000) 
        { 
            return RedirectToAction ("FileTooBig"); 
        } 
        var fileName = Path.GetFileName (file.FileName); 
        var path = Path.Combine (Server.MapPath ("~ / App_Data / uploads"), fileName); 
        file.SaveAs (path); 
    } 
    return RedirectToAction ("Index"); 
} 

If I attach files over 2 megabytes DefaultRedirect works perfectly in Opera, but does not work in Chrome and IE. I also used Response.Redirect ("address here") in Application_Error () event in global.asax. It also does not work in Chrome and IE. What should I do?


maxRequestLength is in Kilobytes (KB). You have yours set to 2000KB (which is slightly less than 2MB as there are 1024KB in 1MB).

I'm not sure why it is working in some browsers and not others, unless some are compressing the entire upload content and others are not (which I believe is supported by HTTP 1.1).

HTH, Brian


There is no way to prevent the file from being uploaded. IIS receives the entire HTTP request body before passing it to the ASP.NET stack. This includes all parts of your multi-part form post. Because of this ASP.NET really has no chance of interrupting a file from being uploaded by checking the file.ContentLength property.

You can write a custom HTTP module to check the file size but aborting or closing the response before receiving the entire request results in an empty response. Meaning there is no way to fail gracefully.

My suggestion would be do do your file upload in a hidden iframe while implementing the HTTP module. That way if something goes wrong, your main page won't break.

Everyone can join me in thanking Microsoft for this awesome "feature" (queue in the sarcasm).

Thanks Microsoft. Thanks.


Try this. This snippet was tested and worked as expected. In the future try not using var type for string variable. var is a dynamic type and should work for all file types - including array. But try to be specific with file type will help reduce errors.

I normally keep my public files in public folder. So, change it to your folder (eg App_Data)

[HttpPost]
public ActionResult test(HttpPostedFileBase file)
{
    if (file.ContentLength> 4096000)  
        {  
            return RedirectToAction ("FileTooBig");  
        }  

    string fileName = Path.GetFileName(file.FileName);
    string uploadPath = Server.MapPath("~/Public/uploads/" + fileName);

    file.SaveAs(uploadPath);
    return View("Index");
}

Good luck


Then try using the if-the-else. This snippet works. If you like it, vote me.

[HttpPost]
public ActionResult test(HttpPostedFileBase file)
{
    if (file.ContentLength > 400)
    {
        return RedirectToAction("GeneralError", "Error");
    }
    else
    {
        string fileName = Path.GetFileName(file.FileName);
        string uploadPath = Server.MapPath("~/Public/uploads/" + fileName);

        file.SaveAs(uploadPath);
        return View("Index");
    }
}

two input buttons required: For browsing the file: For submitting the file:

Good Luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜