文件上传控制仅上传PDF [英] File upload control to upload PDF only

查看:634
本文介绍了文件上传控制仅上传PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个文件控制,如

 < div class =form-group> 
@ Html.LabelFor(m => m.File,new {@class =col-md-2 control-label})
< div class =col-md-10 >
@ Html.TextBoxFor(m => m.File,new {type =file})

< / div>

我希望它只允许PDF格式的文件,所以在我的模型里,就像


$ b $

  [显示(名称=商业条款)] 
[必需,FileExtensions(扩展=。pdf,ErrorMessage = 不正确的文件格式)]
public HttpPostedFileBase File {get;组;然而,控制仍然允许上传任何格式的文件,为什么呢?$ / $> b
$ b

我错过了什么?

解决方案

尝试正则表达式。 b
$ b

  [RegularExpression(@^(([a-zA-Z] :) |(\\ {2} \w +)\\ \\ $?)(\\(\ w [\w]。*))(。pdf | .PDF)$,ErrorMessage =错误的文件格式)] 

确保你有 jquery.validate.js jquery.validate.unobtrusive.js 在页面上引用以启用客户端验证。


I got a file control like

 <div class="form-group">
        @Html.LabelFor(m => m.File, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.File, new { type = "file" })

        </div>

I want it to allow only PDF format files, so in my model, its like

 [Display(Name = "Terms of Business")]
        [Required, FileExtensions(Extensions=".pdf", ErrorMessage="Incorrect file format")] 
        public HttpPostedFileBase File { get; set; }

However, the control still allows to upload documents of any format, why ?

What did I missed out ?

解决方案

Try regular expressions.

[RegularExpression(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF)$", ErrorMessage = "Incorrect file format")]

And make sure you have jquery.validate.js and jquery.validate.unobtrusive.js referenced on the page for enabling client side validation.

这篇关于文件上传控制仅上传PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆