I need to validate the file which is to be uploaded to the server. The validation must be done before uploading it. i.e., validation completed at client side. This task should be accomplished in ASP.NET MVC3 Webpage. It should also work with all browsers. IE9,8,7/FF/Chrome. I came to know that IE doesn't have FileReader API.
My Question is, How to Validate file size before Uploading in a MVC3 Webpage.
.Net MVC Solution:
I am using the data type of
HttpPostedFileBase
In your
Views > Shared
Folder, create a new folder called "EditorTemplates" and use this:I then pass this
HttpPostedFileBase
object from the controller to a method that does the following:The ContentLength property on the HttpPostedFileBase class contains the number of bytes in the posted file
This will make it so you have a file uploading box available.
On the ASP.NET WebForms Solution:
Make a button with a OnClick or OnCommand event that does something like this:
That will give you the file size. Hope this helps.
When it comes for a browser that supports HTML 5, it can be easily achieved with simple javascript:
Html Syntax
Javascript syntax
BUT, when it comes to an older browser (and we are all looking to you, Internet Explorer), the only way to do that on the client side is by using ActiveX:
You can achieve by using jquery:
# #