【问题标题】:Regular expression for image upload not accepting image with mached regular expression图片上传的正则表达式不接受带有匹配正则表达式的图片
【发布时间】:2020-04-10 15:41:44
【问题描述】:

我在asp.net mvc中使用正则表达式作为数据注解为:

  [RegularExpression(@"(.*\.)(jpg|JPG|gif|GIF|png|PNG)$", ErrorMessage = "Only Image files allowed.")]

    public string LabregCertificateLocation { get; set; }

视图如下:

     <div class="col-md-3">
            <div class="form-group">
                <label class="control-label ">Lab Registration Certificate</label>
                 @Html.TextBoxFor(m=>m.LabregCertificateLocation,"",new{ type="file",@class="form-control", @name="LabFile" })
                @Html.ValidationMessageFor(model => model.LabregCertificateLocation, "", new { @class = "text-danger" })
            </div>
        </div>

上传正则表达式中指定类型的图像时收到错误消息。

【问题讨论】:

  • 当你说错误信息时,是验证错误“只允许图像文件”吗?
  • 是的,验证错误“只允许图像文件”
  • 有没有试过调试,在Model.IsValid之前,LabregCertificateLocation的值是多少?
  • 文档说,RegularExpressionAttribute 需要完全匹配
  • 我将“System.Web.HttpPostedFileWrapper”作为 LabregCertificateLocation 值

标签: c# asp.net regex asp.net-mvc


【解决方案1】:

input type="file" 不会向控制器返回字符串。它返回HttpPostedFile。使用 JavaScript 正则表达式在客户端验证文件类型,检查该字段的值。

HttpPostedFile

【讨论】:

    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多