.net - Max file size validation attribute is not working in mvc 4 -
my problem if upload file more 4 mb throwing exception "maximum request length exceeded."
my requirements that: should show validation error message.i not sure whether i'm wrong. please me one
thanks
public class filesizeattribute : validationattribute { private long _maxsize; /// <summary> /// default constructor. defines maximum size of file. /// </summary> public filesizeattribute() { _maxsize = convert.toint32(configurationmanager.appsettings["filesize"]); } /// <summary> /// override isvalid method validate decorated property /// </summary> /// <param name="value"></param> /// <returns></returns> public override bool isvalid(object value) { var file = value httppostedfilebase; if (file == null) return true; return file.contentlength <= _maxsize; } /// <summary> /// override format message method return failure message. /// </summary> /// <param name="name"></param> /// <returns></returns> public override string formaterrormessage(string name) { return string.format("the file size should not exceed {0} mb", math.ceiling((_maxsize / 1024f) / 1024f)); } }
property model:
[filesize] [filetypes] public httppostedfilebase file
in web config file
<httpruntime maxrequestlength="10240" targetframework="4.5" />
use maxfilesize
in model property directly
[required] [maxfilesize(10 * 1024 , errormessage = "maximum allowed file size {0} bytes")]
or
see this
Comments
Post a Comment