File Upload With Strongly Typed View and Model Validation
File Upload With Strongly Typed View and Model Validation
Ke y wo rd s : v a l i d a t e fi l e u s i n g d a t a a n n o t a t i o n ,u p l o a d fi l e i n mv c4 i n s t ro n g l y t y p e d v i e w,fi l e
v a l i d a t i o n u s i n g jq u e ry i n mv c3 mv c4 i n s t ro n g l y t y p e d v i e w
M any times, we required to upload file with strongly-typed view and also apply validation on uploading file using
data annotation validators. In this article, I would like to share, how can we upload a file and validate that file,
firstly at client side and after that at server side.
How to do it..
Step 1 : Designing model with data annotation validation
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 1/6
6/27/2014 File upload with strongly typed view and model validation
(!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
31. {
32. ErrorMessage = "Please upload Your Photo of type: " + string.Join(", ",
AllowedFileExtensions);
33. return false;
34. }
35. else if (file.ContentLength > MaxContentLength)
36. {
37. ErrorMessage = "Your Photo is too large, maximum allowed size is : " + (MaxContentLength
/ 1024).ToString() + "MB";
38. return false;
39. }
40. else
41. return true;
42. }
43. }
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 2/6
6/27/2014 File upload with strongly typed view and model validation
1. <script type="text/jscript">
2. //get file size
3. function GetFileSize(fileid) {
4. try {
5. var fileSize = 0;
6. //for IE
7. if ($.browser.msie) {
8. //before making an object of ActiveXObject,
9. //please make sure ActiveX is enabled in your IE browser
10. var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" +
fileid)[0].value;
11. var objFile = objFSO.getFile(filePath);
12. var fileSize = objFile.size; //size in kb
13. fileSize = fileSize / 1048576; //size in mb
14. }
15. //for FF, Safari, Opeara and Others
16. else {
17. fileSize = $("#" + fileid)[0].files[0].size //size in kb
18. fileSize = fileSize / 1048576; //size in mb
19. }
20.
21. return fileSize;
22. }
23. catch (e) {
24. alert("Error is :" + e);
25. }
26. }
27.
28. //get file path from client system
29. function getNameFromPath(strFilepath) {
30. var objRE = new RegExp(/([^\/\\]+)$/);
31. var strName = objRE.exec(strFilepath);
32.
33. if (strName == null) {
34. return null;
35. }
36. else {
37. return strName[0];
38. }
39. }
40.
41. $(function () {
42. $("#file").change(function () {
43. var file = getNameFromPath($(this).val());
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 3/6
6/27/2014 File upload with strongly typed view and model validation
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 4/6
6/27/2014 File upload with strongly typed view and model validation
15. mRegister.file.SaveAs(path);
16. ViewBag.Message = "File has been uploaded successfully";
17. ModelState.Clear();
18. }
19. return View();
20. }
How it works...
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 5/6
6/27/2014 File upload with strongly typed view and model validation
Print Article
Tw eet
Shailendra Chauhan works as Software Analyst at reputed MNC and has more than 5 years of hand over Microsoft
.NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnet-tricks.com and
www.dotnetinterviewtricks.com blogs. He is an author of book ASP.NET MVC Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#,
SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js,
Angular.js and many more web technologies. More...
http://www.dotnet-tricks.com/Tutorial/mvc/aX9D090113-File-upload-with-strongly-typed-view-and-model-validation.html 6/6