Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check file size on upload

Tags:

c#

asp.net

Whats the best way to check the size of a file during upload using asp.net and C#? I can upload large files by altering my web.config without any problems. My issues arises when a file is uploaded which is more than my allowed max file size.

I have looked into using activex objects but that is not cross browser compatible and not the best answer to the solution. I need it to be cross browser compatible if possible and to support IE6 (i know what you are thinking!! However 80% of my apps users are IE6 and this is not going to change anytime soon unfortunately).

Has any dev out there come across the same problem? And if so how did you solve it?

like image 909
Cragly Avatar asked Jul 05 '09 20:07

Cragly


People also ask

How can check upload file size in PHP?

The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.

How do you show file size in HTML?

The id="size" element will be used to display the size of selected file from the id="upload" element. We listen on the change event of the file input, and get the selected files via e. target.


1 Answers

If you are using System.Web.UI.WebControls.FileUpload control:

MyFileUploadControl.PostedFile.ContentLength;

Returns the size of the posted file, in bytes.

like image 67
Andreas Grech Avatar answered Sep 26 '22 21:09

Andreas Grech