473,480 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How check filesize using FileUpload control?

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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & " MB."
Exit Sub
End If
Jan 18 '06 #1
8 20163
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & " MB."
Exit Sub
End If

Jan 18 '06 #2
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <br******************@safeco.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & " MB."
Exit Sub
End If


Jan 18 '06 #3
Hey y'all look what I found:
https://msdn.microsoft.com/asp.net/d...UploadASP2.asp

"VB Programmer" <do**@emailme.com> wrote in message
news:uc*************@TK2MSFTNGP12.phx.gbl...
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <br******************@safeco.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & "
MB."
Exit Sub
End If



Jan 18 '06 #4
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey y'all look what I found:
https://msdn.microsoft.com/asp.net/d...UploadASP2.asp

"VB Programmer" <do**@emailme.com> wrote in message
news:uc*************@TK2MSFTNGP12.phx.gbl...
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <br******************@safeco.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & "
MB."
Exit Sub
End If



Jan 18 '06 #5
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.

"VB Programmer" <do**@emailme.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey y'all look what I found:
https://msdn.microsoft.com/asp.net/d...UploadASP2.asp

"VB Programmer" <do**@emailme.com> wrote in message
news:uc*************@TK2MSFTNGP12.phx.gbl...
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <br******************@safeco.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
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("MaxVideoUploadSi ze") Then
lblErr.Text = "File needs to be > 0 bytes and less than " &
ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & "
MB."
Exit Sub
End If



Jan 18 '06 #6
True. :)

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Ou*************@TK2MSFTNGP09.phx.gbl...
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.

"VB Programmer" <do**@emailme.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey y'all look what I found:
https://msdn.microsoft.com/asp.net/d...UploadASP2.asp

"VB Programmer" <do**@emailme.com> wrote in message
news:uc*************@TK2MSFTNGP12.phx.gbl...
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <br******************@safeco.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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)
"VB Programmer" <do**@emailme.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
>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("MaxVideoUploadSi ze") Then
> lblErr.Text = "File needs to be > 0 bytes and less than " &
> ConfigurationManager.AppSettings("MaxVideoUploadSi ze") / 1000000 & "
> MB."
> Exit Sub
> End If
>
>



Jan 19 '06 #7
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.
Jan 27 '06 #8
Thanks Gordon!

"Gordon" <Go****@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.com...
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.

Jan 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1957
by: John Boghossian | last post by:
I know this isnt spot on for this group, but the problem relates to an asp.net project i am working on. I am trying to develop an windows forms control to be used in IE in an intranet solution....
15
4709
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
2
2914
by: Islamegy | last post by:
I'm use FormView for editing a photo gallery... I add FileUpload control to upload images to the Gallery. I'm binding my Formview to objectdatasource i tried upload images onItemInserting &...
3
5151
by: rn5a | last post by:
The ASPX FileUpload control displays a TextBox along with a 'Browse...' Button. Setting the different properties of this control just reflects the changes in the TextBox but not the Button. For...
1
1957
by: Nathan Sokalski | last post by:
I have an ASP.NET 2.0 webform which includes both a FileUpload contol and a Calendar control. When the user selects a new data with the Calendar control, the FileUpload control is reset (there is...
3
8643
by: Nathan Sokalski | last post by:
I have a FileUpload control which is inside an UpdatePanel which is inside a TabPanel. In my code, I use the FileUpload's HasFile and FileContent.Length properties. However, after I select a file,...
9
9712
by: vital | last post by:
Hi, I have a User control which has a fileupload control. With the requirements I placed the user control in a UpdatePanel of ASP.NET Ajax and the fileupload.postedfile.filename giving error. ...
2
5971
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a gridview with 2 columns. One column is a BoundColumn to a part number (string). One column is an ItemTemplate with a FileUpload control. There can be multiple rows (i.e. part numbers)...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.