473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 20154
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.