English 中文(简体)
ASP. NET MVC: 处理超重负荷
原标题:ASP.NET MVC: Handling upload exceeding maxRequestLength

在我MVC站点上载一个档案时,Im 想处理由于用户超过最大要求而导致的上载故障,比例如显示通用海关错误页更为宽松。 我更喜欢显示他们试图上台的同一页,但有一个信息告诉他们,他们的档案太大......类似于他们可能发现的错误。

I m starting with the idea from this question:

“请求量超过”

我想要做的是,BUT不是向一个错误的网页转移(正如在这个问题上所做的那样),我想把处理交给原来的控制人,而是在示范国家添加一个错误,表明这一问题。 这里有几部法典,其中评论表明我愿意做什么。 见上文关于IsMaxRequest ExceedEhirion的定义问题,这是一个黑板的范畴,但我有一点认为情况较好。

第1行评论了用户返回右页的情况,但当然他们会失去他们可能作出的任何变化,我不想在这里使用回头。

if (IsMaxRequestExceededException(Server.GetLastError()))
{
    Server.ClearError();
    //((HttpApplication) sender).Context.Response.Redirect(Request.Url.LocalPath + "?maxLengthExceeded=true");
    // TODO: Replace above line - instead tranfer processing to appropriate controlller with context intact, etc
    // but with an extra error added to ModelState.
}

仅仅寻求想法,而不是全面的解决办法;我想要做什么甚至是不可能的?

最佳回答

这里的工作是:在您的网站上建立<代码>最大限度RequestLength 财产。 具有很高的价值。 之后写出习俗验证的属性:

public class MaxFileSize : ValidationAttribute
{
    public int MaxSize { get; private set; }

    public MaxFileSize(int maxSize)
    {
        MaxSize = maxSize;
    }

    public override bool IsValid(object value)
    {
        var file = value as HttpPostedFileBase;
        if (file != null)
        {
            return file.ContentLength < MaxSize;
        }
        return true;
    } 
}

可将您的观点模式与以下几种模式脱节:

public class MyViewModel
{
    [Required]
    [MaxFileSize(1024 * 1024, ErrorMessage = "The uploaded file size must not exceed 1MB")]
    public HttpPostedFileBase File { get; set; }
}

那么,你可以有一个控制人:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // TODO: Process the uploaded file: model.File

        return RedirectToAction("Success");
    }
}

最后一种观点:

@model MyViewModel

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
       @Html.LabelFor(x => x.File)
       <input type="file" name="file" />
       @Html.ValidationMessageFor(x => x.File)
    </div>

    <p><input type="submit" value="Upload"></p>
}
问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签