Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have to save a document in MS SQL server database in column of varBinary(max) format. My Problem is that before saving the document I have to check the size of the file because each user has a 2MB size limit. How can take file size from ASP.NET FileUpload.

Thanks in advance!
Posted
Updated 21-Jan-11 2:19am
v2

You can set the maximum file size allowed in your web.config by modifying the httpRunTime element

http://programming.top54u.com/post/ASP-Net-FileUpload-Size-Limit-Example.aspx[^]

e.g
<httpRuntime maxRequestLength="2048" />


You can also have a look at something like in this thread

http://bytes.com/topic/asp-net/answers/446834-how-check-filesize-using-fileupload-control[^]

VB
Protected Sub Upload_Click () Handles As Upload.Click
If FileUpload1.PostedFile.ContentLength > 10240 then
    lblMessage.Text = "File is too large."
Else
    FileUpload1.PostedFile.SaveAs(Location)
End If
End Sub


You can't reliably get the file size from any client scripting languages, file manipulation on the client would be a large security hole.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-Jan-11 8:21am    
Good call! 5+
Ankur\m/ 21-Jan-11 8:25am    
file manipulation on the client would be a large security hole
Google does it in Gmail. I think topic was discussed here long back and someone told how Google does it. Let me search it.
Dylan Morley 21-Jan-11 8:33am    
Probably going to be something like using swf and passing it through a flash variable.

I'm sure there are ways, but that's why I said you can't do it reliably. You don't know if the user is allowing flash content (I use flashblock on FF).

I'm sure you'd explicitly need to allow some sort of access to your FileSystem, otherwise it could wreak havoc!
Ankur\m/ 21-Jan-11 8:44am    
That's correct. AFAIK, Google uses flash to do it. But as I said, this topic was discussed long back and someone gave a link on how Google does it and IIRC it was without flash. I will try to find out that post, but I am not sure it was here or in the Lounge. Let me search and see.
Apart from what Dylan already said,

You can use third party controls for this purpose. Some of them are:

Ajax Uploader
SWFUpload
RAD Upload

Hope that helps!
 
Share this answer
 
v2

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