Thursday, March 29, 2012

Maximum file upload question

In ASP.Net (VS 2005), is there a way to limit the user to upload a
file up to certain size (say, I would like to limit the user to upload
at the maximum 1 meg of file) ?
Thank youSince there is no way for you to tell from the server-side how big the user'
s
file is, you simply check the UploadedFile object's size at the server and
reject it with the appropriate message to the user.
--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com
"fiefie.niles@.gmail.com" wrote:

> In ASP.Net (VS 2005), is there a way to limit the user to upload a
> file up to certain size (say, I would like to limit the user to upload
> at the maximum 1 meg of file) ?
> Thank you
>
I suppose Peter was talking about HttpPostedFile.ContentLength property :)
WBR, Michael Nemtsev [.NET/C# MVP].
Blog: http://spaces.live.com/laflour
"fiefie.niles@.gmail.com" wrote:

> In ASP.Net (VS 2005), is there a way to limit the user to upload a
> file up to certain size (say, I would like to limit the user to upload
> at the maximum 1 meg of file) ?
> Thank you
>
re:
!> I would like to limit the user to upload at the maximum 1 meg of file
Iy's very simple...
In web.config :
<httpRuntime maxRequestLength="1024"/>
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
<fiefie.niles@.gmail.com> wrote in message news:28988e20-c2f8-4abe-b25a-32e5a334e155@.l22g200
0hsc.googlegroups.com...
> In ASP.Net (VS 2005), is there a way to limit the user to upload a
> file up to certain size (say, I would like to limit the user to upload
> at the maximum 1 meg of file) ?
> Thank you
re:
!> Since there is no way for you to tell from the server-side how big the us
er's file is
There isn't any way to do that, but you can limit the maximum size for any u
ploaded file.
See my just-sent-in tip.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.NoSpamMaam.com> wrote in message
news:FD220D8D-0CE2-4D94-9D0E-12A497AF8F1C@.microsoft.com...
> Since there is no way for you to tell from the server-side how big the use
r's
> file is, you simply check the UploadedFile object's size at the server an
d
> reject it with the appropriate message to the user.
> --Peter
> "Inside every large program, there is a small program trying to get out."
> http://www.eggheadcafe.com
> http://petesbloggerama.blogspot.com
> http://www.blogmetafinder.com
>
> "fiefie.niles@.gmail.com" wrote:
>
It should be noted that this entry will limit the size of all HTTP requests
for all pages controlled by that web.config. You can, however, put the
"upload file" page in its own sub-folder and put a web.config in that folder
with the appropriate settings. For example, you might also want to adjust
the timeout value to allow for slow uploads, etc.
I guess my point is, the upload settings for files is probably different
than for "regular" requests, so consider putting pages that upload files
into their own sub-folder with their own web.config.

> !> I would like to limit the user to upload at the maximum 1 meg of file
> Iy's very simple...
> In web.config :
> <httpRuntime maxRequestLength="1024"/>
Hmmm... a bit nitpicky, but true.
I say nitpicky because I've been trying to figure out what an http request l
arger than
1MB looks like, other than an uploaded file...and can't come up with a valid
example.
However, for comprehensiveness sake, ( just in case a request is made which
isn't
an uploaded file but is larger than 1MB ) the following web.config entry wou
ld cover all bases :
<configuration>
<location path="UploadPage.aspx">
<httpRuntime maxRequestLength="1024"/>
</location>
</configuration>
Notice that the entry can be located in the root web.config.
A separate web.config is not needed.
If the upload aspx is in a different directory, including the path will do f
ine :
<location path="/directory/UploadPage.aspx">
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Scott Roberts" <sroberts@.no.spam.here-webworks-software.com> wrote in messa
ge
news:u2F1T%23lKIHA.5328@.TK2MSFTNGP05.phx.gbl...
> It should be noted that this entry will limit the size of all HTTP request
s for all pages controlled by that
> web.config. You can, however, put the "upload file" page in its own sub-fo
lder and put a web.config in that folder
> with the appropriate settings. For example, you might also want to adjust
the timeout value to allow for slow uploads,
> etc.
> I guess my point is, the upload settings for files is probably different t
han for "regular" requests, so consider
> putting pages that upload files into their own sub-folder with their own w
eb.config.
>
>
Juan,
There probably aren't *valid* requests larger than 1MB that are not file
uploads. Although, if you're not careful the viewstate can get pretty close
to that pretty quick (but then you've got other problems).
It's not so much the request size that I'm concerned about, but the request
timeout. I like to set it pretty high for file uploads, but not so high for
normal pages. Plus, we allow file uploads much larger than 1MB, so I was
really just commenting on file uploads in general.
Thanks for your tips, we may switch to the "location" attribute in order to
keep all config items in one place.
Scott
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:eV4IPepKIHA.4228@.TK2MSFTNGP02.phx.gbl...
> Hmmm... a bit nitpicky, but true.
> I say nitpicky because I've been trying to figure out what an http request
> larger than
> 1MB looks like, other than an uploaded file...and can't come up with a
> valid example.
> However, for comprehensiveness sake, ( just in case a request is made
> which isn't
> an uploaded file but is larger than 1MB ) the following web.config entry
> would cover all bases :
> <configuration>
> <location path="UploadPage.aspx">
> <httpRuntime maxRequestLength="1024"/>
> </location>
> </configuration>
> Notice that the entry can be located in the root web.config.
> A separate web.config is not needed.
> If the upload aspx is in a different directory, including the path will do
> fine :
> <location path="/directory/UploadPage.aspx">
re:
!> It's not so much the request size that I'm concerned about, but the reque
st timeout.
!> I like to set it pretty high for file uploads, but not so high for normal
pages.
That makes sense...
re:
!> we may switch to the "location" attribute in order to keep all config ite
ms in one place
Yes, doing that keeps config items nice and tidy in one place.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Scott Roberts" <sroberts@.no.spam.here-webworks-software.com> wrote in messa
ge
news:O%23iIL7rKIHA.2432@.TK2MSFTNGP04.phx.gbl...
> Juan,
> There probably aren't *valid* requests larger than 1MB that are not file u
ploads. Although, if you're not careful the
> viewstate can get pretty close to that pretty quick (but then you've got o
ther problems).
> It's not so much the request size that I'm concerned about, but the reques
t timeout. I like to set it pretty high for
> file uploads, but not so high for normal pages. Plus, we allow file upload
s much larger than 1MB, so I was really just
> commenting on file uploads in general.
> Thanks for your tips, we may switch to the "location" attribute in order t
o keep all config items in one place.
> Scott
>
> "Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message news:eV4IPep
KIHA.4228@.TK2MSFTNGP02.phx.gbl...
>

No comments:

Post a Comment