我在我的MVC应用程序中实现了一个图像上传选项但是我一直收到以下错误:找不到类型或命名空间名称'FileSizeAttribute'。
从下面的代码中我可以看到,我正在实现cusom属性来确定允许的文件大小和类型。
public class UploadImageModel
{
[FileSize(10240)]
[FileTypes("jpg,jpeg,png")]
public HttpPostedFileBase File { get; set; }
}
我在ProfileController.cs中定义了这些属性,你可以在这里看到。
public class FileSizeAttribute : ValidationAttribute
{
private readonly int _maxSize;
public FileSizeAttribute(int maxSize)
{
_maxSize = maxSize;
}
public override bool IsValid(object value)
{
if (value == null) return true;
return _maxSize > (value as HttpPostedFile).ContentLength;
}
public override string FormatErrorMessage(string name)
{
return string.Format("The image size should not exceed {0}", _maxSize);
}
}
public class FileTypesAttribute : ValidateInputAttribute
{
private readonly List<string> _types;
public FileTypesAttribute(string types)
{
_types = types.Split(',').ToList();
}
public override bool IsValid(object value)
{
if (value == null) return true;
var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
}
public override string FormatErrorMessage(string name)
{
return string.Format("Invalid file type. Only the following types {0} are supported.",
String.Join(", ", _types));
}
}
[HttpPost]
public ActionResult PhotoUpload(UploadImageModel imageModel)
{
string path = @"D:\Temp\";
if (ModelState.IsValid)
{
if (imageModel != null && imageModel.File != null)
image.SaveAs(path + imageModel.File.FileName);
return RedirectToAction("Profile");
}
return View();
}
我应该提一下,我正在调整此article的代码,因为我是MVC的新手并且正在学习绳索。也就是说,我需要在头文件中包含一个using子句,引用我的Controller,还是我缺少的语法问题?在此先感谢您的帮助。
答案 0 :(得分:2)
让我们假设您至少将这些属性移到一个组合文件中,称之为ExtensionAttributes.cs
。在该文件中,您将拥有类似
namespace artisan.Attributes
{
public class FileSizeAttribute : ValidationAttribute
{
private readonly int _maxSize;
public FileSizeAttribute(int maxSize)
{
_maxSize = maxSize;
}
public override bool IsValid(object value)
{
if (value == null) return true;
return _maxSize > (value as HttpPostedFile).ContentLength;
}
public override string FormatErrorMessage(string name)
{
return string.Format("The image size should not exceed {0}", _maxSize);
}
}
public class FileTypesAttribute : ValidateInputAttribute
{
private readonly List<string> _types;
public FileTypesAttribute(string types)
{
_types = types.Split(',').ToList();
}
public override bool IsValid(object value)
{
if (value == null) return true;
var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
}
public override string FormatErrorMessage(string name)
{
return string.Format("Invalid file type. Only the following types {0} are supported.",
String.Join(", ", _types));
}
}
}
然后,要在模型上使用该属性,您可以执行类似
的操作using artisan.Attributes;
public class UploadImageModel
{
[FileSize(10240)]
[FileTypes("jpg,jpeg,png")]
public HttpPostedFileBase File { get; set; }
}
否则,按原样,您需要向模型类添加artisan.Controllers
,但是(给定默认项目模板)可能会导致一些循环引用。