Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How check filesize using FileUpload control?

Reply
Thread Tools

How check filesize using FileUpload control?

 
 
VB Programmer
Guest
Posts: n/a
 
      01-18-2006
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


 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      01-18-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>
>



 
Reply With Quote
 
 
 
 
VB Programmer
Guest
Posts: n/a
 
      01-18-2006
My users are going to upload video files, up to 10MB each. What are my
alternatives (FREE preferred)....

"Bruce Barker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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
>>
>>

>
>



 
Reply With Quote
 
VB Programmer
Guest
Posts: n/a
 
      01-18-2006
Hey y'all look what I found:
https://msdn.microsoft.com/asp.net/d...UploadASP2.asp

"VB Programmer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My users are going to upload video files, up to 10MB each. What are my
> alternatives (FREE preferred)....
>
> "Bruce Barker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Bruce Barker
Guest
Posts: n/a
 
      01-18-2006
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hey y'all look what I found:
> https://msdn.microsoft.com/asp.net/d...UploadASP2.asp
>
> "VB Programmer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> My users are going to upload video files, up to 10MB each. What are my
>> alternatives (FREE preferred)....
>>
>> "Bruce Barker" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>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
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      01-18-2006
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hey y'all look what I found:
> https://msdn.microsoft.com/asp.net/d...UploadASP2.asp
>
> "VB Programmer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> My users are going to upload video files, up to 10MB each. What are my
>> alternatives (FREE preferred)....
>>
>> "Bruce Barker" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>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
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
VB Programmer
Guest
Posts: n/a
 
      01-18-2006
True.

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hey y'all look what I found:
>> https://msdn.microsoft.com/asp.net/d...UploadASP2.asp
>>
>> "VB Programmer" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> My users are going to upload video files, up to 10MB each. What are my
>>> alternatives (FREE preferred)....
>>>
>>> "Bruce Barker" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>>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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?R29yZG9u?=
Guest
Posts: n/a
 
      01-27-2006
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.
 
Reply With Quote
 
VB Programmer
Guest
Posts: n/a
 
      01-27-2006
Thanks Gordon!

"Gordon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.



 
Reply With Quote
 
vijendradudhe@gmail.com vijendradudhe@gmail.com is offline
Junior Member
Join Date: Aug 2012
Posts: 12
 
      09-01-2012
Quote:
Originally Posted by VB Programmer View Post
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
77777777777777777777777777777
 

Last edited by vijendradudhe@gmail.com; 09-01-2012 at 08:56 AM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
display filesize javascript using KDE konqueror linbox Javascript 0 01-20-2007 04:57 PM
get filesize using javascript trapman Javascript 3 04-19-2006 10:51 PM
check filesize before reading it Magix C Programming 4 07-06-2004 01:57 PM
Filesize limitations using cdoSendUsingPickup, cdoSendUsingPort Alan Howard ASP General 2 06-20-2004 12:44 AM
Format filesize in kB or MB Arjen ASP .Net 2 08-09-2003 03:56 AM



Advertisments