Previously wrote an article: MVC: Uploading Files
Add the next feature today: How to limit upload file types
File types can be limited in the previous paragraph, but are too easy to bypass, preferably in the back-end processing.
To modify the Upload method code:
[HttpPost] Publicactionresult Upload (httppostedfilebase file) {if(File = =NULL) { returnContent ("No files! "); }
//------------------------------------------------- varthisextension=path.getextension (file. FileName); string[] Allowextension =New string[] {". xls",". xlsx" }; if(!allowextension.contains (thisextension)) { returnContent ("The file type is not correct. "); } //-------------------------------------------------
varfilename = Path.Combine (Request.mappath ("~/upload"), Path.getfilename (file. FileName)); File. SaveAs (filename); returnContent ("OK");}
MVC: Restricting file types when uploading files