问题描述
我尝试在MVC中使用HTML FileUpload控件上传文件。我想验证文件只接受特定的扩展名。我曾尝试使用DataAnnotations命名空间的FileExtensions属性,但它不工作。请参阅下面的代码 - public class FileUploadModel
{
[必需,FileExtensions(Extensions = xlsx,.xls),ErrorMessage =请选择一个Excel文件。)]
public HttpPostedFileBase File {get;组; }
$
$ p $在控制器中,我写的代码如下 - [HttpPost]
public ActionResult Index(FileUploadModel fileUploadModel)
{
if(ModelState.IsValid)
fileUploadModel.File.SaveAs(Path.Combine(Server.MapPath(〜/ UploadedFiles),Path.GetFileName(fileUploadModel.File.FileName)));
return View();
在视图中,我写下了代码 - $ /
@using(Html.BeginForm(Index,FileParse,FormMethod.Post,new {enctype =multipart / form-data}))
{
@ Html.Label(Upload Student Excel:)
< input type =filename =fileid =file/>
< input type =submitvalue =Import/>
@ Html.ValidationMessageFor(m => m.File)
}
当我运行应用程序并给出一个无效的文件扩展名,它不显示我的错误信息。
我知道编写自定义验证属性的解决方案,但我不想使用自定义属性。请指出我出错的地方。
解决方案我有同样的问题,我解决了创建一个新的ValidationAttribute。 >
像这样:
pre $ { AllowMultiple = false,Inherited = true)]
public class FileExtensionsAttribute:ValidationAttribute
{
private List< string> AllowedExtensions {get;组; }
$ b public FileExtensionsAttribute(string fileExtensions)
{
AllowedExtensions = fileExtensions.Split(new char [] {','},StringSplitOptions.RemoveEmptyEntries).ToList();
}
public override bool IsValid(object value)
{
HttpPostedFileBase file = value as HttpPostedFileBase;
if(file!= null)
{
var fileName = file.FileName;
return AllowedExtensions.Any(y => fileName.EndsWith(y));
}
return true;
$ b现在,只需使用这个:
[b]文件扩展名(jpg,jpeg,png,gif,ErrorMessage =您的错误信息。]]
public HttpPostedFileBase Imagem {得到;组; }
我帮了忙。
拥抱!
I am trying to upload a file using HTML FileUpload control in MVC. I want to validate the file to accept only specific extensions. I have tried using FileExtensions attribute of DataAnnotations namespace, but its not working. See code below -
public class FileUploadModel
{
[Required, FileExtensions(Extensions = (".xlsx,.xls"), ErrorMessage = "Please select an Excel file.")]
public HttpPostedFileBase File { get; set; }
}
In the controller, I am writing the code as below -
[HttpPost]
public ActionResult Index(FileUploadModel fileUploadModel)
{
if (ModelState.IsValid)
fileUploadModel.File.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), Path.GetFileName(fileUploadModel.File.FileName)));
return View();
}
In View, I have written below code -
@using (Html.BeginForm("Index", "FileParse", FormMethod.Post, new { enctype = "multipart/form-data"} ))
{
@Html.Label("Upload Student Excel:")
<input type="file" name="file" id="file"/>
<input type="submit" value="Import"/>
@Html.ValidationMessageFor(m => m.File)
}
When i run the application and give an invalid file extension, its not showing me the error message.I am aware of solution to write custom validation attribute, but I dont want to use custom attribute. Please point out where I am going wrong.
解决方案 I had the same problem and I resolved creating a new ValidationAttribute.
Like this:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FileExtensionsAttribute : ValidationAttribute
{
private List<string> AllowedExtensions { get; set; }
public FileExtensionsAttribute(string fileExtensions)
{
AllowedExtensions = fileExtensions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
public override bool IsValid(object value)
{
HttpPostedFileBase file = value as HttpPostedFileBase;
if (file != null)
{
var fileName = file.FileName;
return AllowedExtensions.Any(y => fileName.EndsWith(y));
}
return true;
}
}
Now, just use this:
[FileExtensions("jpg,jpeg,png,gif", ErrorMessage = "Your error message.")]
public HttpPostedFileBase Imagem { get; set; }
I have helped.Hugs!
这篇关于DataAnnotations的FileExtensions属性在MVC中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!