Remote file extension validation in ASP.NET MVC3 with C#? -


i have mvc3 application uploads file users' hard drive , manipulates it. 1 requirement file extension should .xls(or xlsx).

i validate file name know best option in terms of reusability , performance (and of course best coding practices).

i have following index view:

@using (html.beginform("index", "home", formmethod.post, new { enctype = "multipart/form-data" }))     {         <br />         <p><input type="file" id="file" name="file" size="23"/></p><br />         <p><input type="submit" value="upload file" /></p>             }   

called controller action method index:

public actionresult index() {    return view("index"); } 

and user response handled index action method (httppost):

[httppost] public actionresult index(httppostedfilebase file) {     fileservices lfileservices = new fileservices();     var lfilepath = lfileservices.uploadfile(file, "~/app_data/uploads");     //manipulation; } 

now can use path.getextension(filename) in fileservices extension , perform check performed after upload completes.

i once user attempts upload file. thing came mind create model(or better viewmodel) , use remote validation dataannotations.

i saw demonstration scott hanselman live seemed not confortable because application compiling not performing check.

has approach in order perform such kind of remote validation or other solution (jquery instance)?

thanks

francesco

you using javascript:

$(function () {     $('form').submit(function () {         var selectedfile = $('#file').val();         var matches = selectedfile.match(/\.(xlsx?)$/i);         if (matches == null) {             alert('please select excel file');             return false;         }         return true;     }); }); 

of course doesn't in case free obligation of performing same check on server because if client has no javascript enabled way. , wouldn't 100% reliable there nothing preventing users renaming garbage file .xls , upload it. heuristics used on server try guess actual file type looking @ known byte sequences.


update:

example remote ajax validation (due demand in comments, don't recommend though). use excellent jquery.validate plugin way comes bundled asp.net mvc 3:

<script src="@url.content("~/scripts/jquery.validate.js")" type="text/javascript"></script>  @using (html.beginform("index", "home", formmethod.post, new { enctype = "multipart/form-data" })) {     <input type="file" id="file" name="file" size="23" data-remote-val-url="@url.action("checkextension")"/>     <br />     <input type="submit" value="upload file" /> }  <script type="text/javascript">     $('form').validate({         rules: {             file: {                 remote: $('#file').data('remote-val-url')             }         },         messages: {             file: {                 remote: 'please select excel file'             }         }     }); </script> 

and on server:

public actionresult checkextension(string file) {     var extension = path.getextension(file ?? string.empty);     var validextensions = new[] { ".xls", ".xlsx" };     var isvalid = validextensions.contains(extension, stringcomparer.ordinalignorecase);     return json(isvalid, jsonrequestbehavior.allowget); } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -