上传时验证大文件

Wil*_*eja 30 c# asp.net validation file-upload asp.net-mvc-2

我正在使用c#MVC 2和ASP.NET.我的一个表单包括一个文件输入字段,允许用户选择任何文件类型,然后将其转换为blob并保存到数据库中.我的问题是,每当用户选择超过某个Mb(约8)的文件时,我会收到一个页面错误,说明如下:

The connection was reset
The connection to the server was reset while the page was loading.
Run Code Online (Sandbox Code Playgroud)

我不介意用户上传的文件有8Mb的限制但是我需要停止当前错误的发生并显示正确的验证消息(最好使用ModelState.AddModelError函数).有谁能够帮我?在页面中发生任何其他事情之前,我似乎无法"捕获"错误,因为它在它到达控制器内的上传功能之前发生.

Dar*_*rov 69

一种可能性是编写自定义验证属性:

public class MaxFileSizeAttribute : ValidationAttribute
{
    private readonly int _maxFileSize;
    public MaxFileSizeAttribute(int maxFileSize)
    {
        _maxFileSize = maxFileSize;
    }

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

    public override string FormatErrorMessage(string name)
    {
        return base.FormatErrorMessage(_maxFileSize.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以有一个视图模型:

public class MyViewModel
{
    [Required]
    [MaxFileSize(8 * 1024 * 1024, ErrorMessage = "Maximum allowed file size is {0} bytes")]
    public HttpPostedFileBase File { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // validation failed => redisplay the view
            return View(model);
        }

        // the model is valid => we could process the file here
        var fileName = Path.GetFileName(model.File.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        model.File.SaveAs(path);

        return RedirectToAction("Success");
    }
}
Run Code Online (Sandbox Code Playgroud)

和一个观点:

@model MyViewModel

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(x => x.File, new { type = "file" })
    @Html.ValidationMessageFor(x => x.File)
    <button type="submit">OK</button>
}
Run Code Online (Sandbox Code Playgroud)

当然,为了实现这一点,您必须将web.config中允许的最大上载文件大小增加到足够大的值:

<!-- 1GB (the value is in KB) -->
<httpRuntime maxRequestLength="1048576" />
Run Code Online (Sandbox Code Playgroud)

对于IIS7:

<system.webServer>
    <security>
        <requestFiltering>
           <!-- 1GB (the value is in Bytes) -->
            <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
    </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

我们现在可以将自定义验证属性更进一步,并启用客户端验证以避免浪费带宽.当然,只有使用HTML5 File API才能在上传之前验证文件大小.因此,只有支持此API的浏览器才能利用它.

所以第一步是让我们的自定义验证属性实现IClientValidatable接口,这将允许我们在javascript中附加自定义适配器:

public class MaxFileSizeAttribute : ValidationAttribute, IClientValidatable
{
    private readonly int _maxFileSize;
    public MaxFileSizeAttribute(int maxFileSize)
    {
        _maxFileSize = maxFileSize;
    }

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

    public override string FormatErrorMessage(string name)
    {
        return base.FormatErrorMessage(_maxFileSize.ToString());
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(_maxFileSize.ToString()),
            ValidationType = "filesize"
        };
        rule.ValidationParameters["maxsize"] = _maxFileSize;
        yield return rule;
    }
}
Run Code Online (Sandbox Code Playgroud)

剩下的就是配置自定义适配器:

jQuery.validator.unobtrusive.adapters.add(
    'filesize', [ 'maxsize' ], function (options) {
        options.rules['filesize'] = options.params;
        if (options.message) {
            options.messages['filesize'] = options.message;
        }
    }
);

jQuery.validator.addMethod('filesize', function (value, element, params) {
    if (element.files.length < 1) {
        // No files selected
        return true;
    }

    if (!element.files || !element.files[0].size) {
        // This browser doesn't support the HTML5 API
        return true;
    }

    return element.files[0].size < params.maxsize;
}, '');
Run Code Online (Sandbox Code Playgroud)

  • 真棒的答案!但是有一个问题.当值为null时,MaxFileSizeAttribute.IsValid返回false,实际上需要上载文件. (3认同)
  • 您的客户端javascript验证中有一个小错误.最后一行应该是"return element.files [0] .size <= params.maxsize;" 如果他们正在上传与您的最大尺寸完全相同的文件.否则这非常有帮助. (2认同)
  • 要阻止所需的检查,只需键入"IsValid":if(file == null)return true; (2认同)