Asp.net Basics : Uploading a file in asp.net using validations (Part 3 of 5)

This is a part 3 of the 5 part tutorial.
If you are visiting this page for the first time then you must visit part 1 of the tutorial
on how to upload a file on server here

CoverImageASPNetFileUploadPart3

File Upload in asp.net : v1.2 (File Size Validation)

File upload limit is one of the important controls for file upload control, if file upload control is not provided in uploading of file then there can be huge files uploaded which can kill the server cache. There is neither a javascript function which can provide access to client side and get the file size.

This is basically an upgrade to our v1.1 of our file upload system. So now server can check for whether a file is uploaded and can also check the file size. There are 2 steps for limiting the file size, first we have to declare <httpRuntime> element in our web.config file and then creating a C# code.

The attributes of httpRuntime element are

Step 1 : Configuring the web.config file.

The web.config gives us httpRuntime element. The attribute executionTimeout and maxRequestLength is required for this purpose but you can explore other options also. The default size for maxRequestLength is 4MB. Here is the screenshot of web.config file :-

FileUploadT3.1

here executionTimeout = “999” means that the file will upload for 999 seconds if the upload time is greater than 999 seconds then the upload will fail and connection will reset, and maxRequestLength = “2097151” means that it will upload max of 2097151KB or 2GB.

Step 2 : C# coding.

Since web.config is configured now we have to code the C# file.
The C# code is :-

The output of the above C# code is :-

FileUploadTut3-2

FileUploadTut3-3

That’s all for the FileSize validation. In the next section we will discuss about the FileType or FileExtension validation.


Leave a Reply

Your email address will not be published. Required fields are marked *