Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the size of a file in aspx file upload control..can anyone help me?
Posted
Comments
Hemant Singh Rautela 1-Feb-13 1:42am    
why not your search it first yourself...

A very beginner question, which solution is already on web...

C#
fileUpload1.FileBytes.Length
will give you size in bytes

Example
C#
int sizeinbytes=fileUpload1.FileBytes.Length;


also

you can try

C#
int sizeinbytes= FileUpload1.PostedFile.ContentLength;


Updated to display size in user friendly manner:

C#
string[] sizes = { "B", "KB", "MB", "GB" };
double sizeinbytes=fileUpload1.FileBytes.Length;
int order = 0;
while (sizeinbytes>= 1024 && order + 1 < sizes.Length) {
    order++;
    sizeinbytes= sizeinbytes/1024;
}

// Adjust the format string to your preferences. For example "{0:0.#}{1}" would
// show a single decimal place, and no space.
string result = String.Format("{0:0.##} {1}", sizeinbytes, sizes[order]);


[Edit - Just brought the OPs two answers into one.]
If you want to get file size through javascript,
then check this solution

File size detection[^]
 
Share this answer
 
v8
Comments
Maruthiram_99 1-Feb-13 1:18am    
Do we need to send any arguments with it??can u explain clearly??
Santhosh Kumar Jayaraman 1-Feb-13 1:20am    
this property will give you length..no arguments required. Like this
int sizeinbytes=fileUpload1.FileBytes.Length;
Maruthiram_99 1-Feb-13 1:31am    
If we want the exact size of the file i.e in KB or MB the file is in...How to get it??
Santhosh Kumar Jayaraman 1-Feb-13 1:36am    
updated the solution.please check
C#
int imageSize = myFileUploadControl.PostedFile.ContentLength;


Thanks
 
Share this answer
 
Comments
Maruthiram_99 1-Feb-13 1:31am    
If we want the exact size of the file i.e in KB or MB the file is in...How to get it??
AshishChaudha 1-Feb-13 2:14am    
Please update your question, what exactly you need to resolve..
content length = FileUpload1.PostedFile.ContentLength;

file name legth = FileUpload1.FileName.Length;

where FileUpload1 is a file upload control name..

...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900