How check filesize using FileUpload control?

V

VB Programmer

I am using a FileUpload control (ASP.NET 2.0). How do I check the length of
the file in bytes BEFORE I let them upload it?

I believe my code checks now AFTER...

If FileUpload1.PostedFile.ContentLength >
ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
Exit Sub
End If
 
B

Bruce Barker

you can't unless you write your own active/x control to do the uploads. also
you can not catch file too large, as asp.net just terminates the upload
(closes the socket) to stop at max size, thus no response can be sent.

-- bruce (sqlwork.com)
 
V

VB Programmer

My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....
 
B

Bruce Barker

note: you can do the same 1.1

asp.net fileupload support loads the file into memory, then calls page
processing. as long as you have enough memeory (and up the page timeout),
you can load 10mb files.

there are third party controls that write the file to disk (in a temp
folder) as it comes in to save memory, then passes the filename to the page.

-- bruce (sqlwork.com)
 
K

Kevin Spencer

That doesn't say anything about checking the file size prior to uploading
it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
V

VB Programmer

True. :)

Kevin Spencer said:
That doesn't say anything about checking the file size prior to uploading
it.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
G

Guest

I think I figured out a workaround to solve this issue with checking the file
size PRIOR to upload in ASP.NET 2.0. This will probably work in 1.1 as well.

Let’s say one of your users is trying to upload a 15 MB file but the limit
for upload is 10 MB. When you set the value for <httpRuntime
maxRequestLength="value" /> in web.config, don’t set it at 10 MB but at
something much higher like 20 MB. In your upload page, do a check for the
ContentLength (convert this to KB by dividing it by 1024 and round it up by
using System.Math.Ceiling) of the file against the 10 MB (10 x 1024 = 10240)
limit inside the click event for the upload button. You should be able to
trap the size limit check BEFORE the file is loaded onto the server.

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

What’s happening here is that the 15 MB file will be loaded into the memory
in IIS. But since the maxRequestLength limit is set at 20 MB, you don’t run
into the “Can’t find page†problem. The click event should fire properly
within the application and trap the error prior to saving the file on the
server.

I've tried this with a 50 MB setting on maxRequestLength with multiple
FileUpload controls and it is working fine.

Hope this helps.
 
Joined
Aug 31, 2012
Messages
11
Reaction score
0
I am using a FileUpload control (ASP.NET 2.0). How do I check the length of
the file in bytes BEFORE I let them upload it?

I believe my code checks now AFTER...

If FileUpload1.PostedFile.ContentLength >
ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
Exit Sub
End If
77777777777777777777777777777
 
Last edited:
Joined
Aug 31, 2012
Messages
11
Reaction score
0
I am using a FileUpload control (ASP.NET 2.0). How do I check the length of
the file in bytes BEFORE I let them upload it?

I believe my code checks now AFTER...

If FileUpload1.PostedFile.ContentLength >
ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
Exit Sub
End If

gfffffffffffffffffffffffffffffffffffffffffffffffffff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top