asp.net mvc - DataAnnotation Regular Expression always returns false for file input -


i've tried many regular expressions regularexpression data annotation check if file extension image , returns false e.g. i've tried fileextension attribute creates error on jquery.validation. i'm using asp.net mvc 4 razor

[regularexpression(@"^.*\.(jpg|gif|jpeg|png|bmp)$",  errormessage = "please use image extension of .jpg, .png, .gif, .bmp")] public string myimage { get; set; } 

and markup

    <div class="editor-field">                     @html.textboxfor(x => x.departmentimage, new { type = "file" })                     @html.validationmessage("departmentimageerror")         @html.validationmessagefor(model => model.departmentimage)     </div> 

could show me how make work?

try modifying code below.

@html.validationmessagefor(model => model.myimage) 

my suggestion

your form should below.

@using (html.beginform("acion", "conroller", formmethod.post,                                        new { enctype = "multipart/form-data" })) {     <input type="file" name="fileinfo" value="file upload" />     @html.validationmessagefor(i => i.fileinfo);     <button type="submit" name="upload" value="upload" /> } 

enter image description here

httppostedfilebasemodelbinder

*when have single instance of httppostedfilebase action parameter or property in model mapping file done httppostedfilebasemodelbinder , no value providers used in case. may think why no value providers used in case, it's because source single , clear i.e. request.files collection.*

enter image description here

model

public class uploadfilemodel {     [filesize(10240)]     [filetypes("jpg,jpeg,png")]     public httppostedfilebase fileinfo { get; set; } } 

enter image description here

filesizeattribute

enter image description here

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 httppostedfilebase).contentlength;     }      public override string formaterrormessage(string name)     {         return string.format("the file size should not exceed {0}", _maxsize);     } } 

filetypesattribute

public class filetypesattribute: validationattribute {     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                                      httppostedfilebase).filename).substring(1);         return _types.contains(fileext, stringcomparer.ordinalignorecase);     }      public override string formaterrormessage(string name)     {         return string.format("invalid file type. following types {0}                                      supported.", string.join(", ", _types));     } } 

controller action method

[httppost] public actionresult upload(uploadfilemodel filemodel) {          if(modelstate.isvalid)     {      }      return view(filemodel); } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -