469,924 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

ASP.NET File Upload problem

Hey.

I was experimenting with file uploading possibility using ASP.NET.
I ran into a problem :(

I can upload small files, but if I try a bigger file it doesn't work...

Anyone who can help me please ?

Thanks.
K.
private void btnUpload_Click(object sender, System.EventArgs e)
{
string strBaseLocation = Server.MapPath( "./files" );

if ( txtUploadFile.Value == string.Empty )
{
lblInfo.Text = "Gelieve een bestand te selecteren.";
return;
}

string fileName = GetFileName( txtUploadFile.Value );
string fileTarget = strBaseLocation + "\\" + fileName;

if (null != txtUploadFile.PostedFile)
{
try
{
//read/write buffer
const int BUFFER_SIZE = 255;
Byte[] Buffer = new Byte[BUFFER_SIZE];
lblInfo.Text = "Uploading File: " + fileName;

//incoming external file stream
Stream theStream = txtUploadFile.PostedFile.InputStream;

//local file stream
FileStream fs = new FileStream(fileTarget, FileMode.CreateNew,
FileAccess.Write);

//local binary writer on local file stream
BinaryWriter bw = new BinaryWriter(fs);
// read/write loop
while( theStream.Read(Buffer, 0, BUFFER_SIZE) != 0 )
{
bw.Write(Buffer, 0, BUFFER_SIZE);
}

//Close the streams
bw.Close();
fs.Close();
theStream.Close();
lblInfo.Text = "Upload finished.";

//reload directory listing
FindAndDisplayFiles();
}
catch(Exception ex)
{
lblInfo.Text = "Fout bij het uploaden.<br>" + ex.Message;
}
}
}
Nov 19 '05 #1
15 1648
k, already found that httpRunTime defaults to max 4000 kb...

<configuration>
<system.web>
<httpRuntime maxRequestLength="4000"
useFullyQualifiedRedirectUrl="true"
executionTimeout="45"
versionHeader="1.1.4128"/>
</system.web>
</configuration>

How do I change it ?

Thank you.
K.


"Asshen Shugar" <va********@hotmail.com> wrote in message
news:6H*********************@phobos.telenet-ops.be...
Hey.

I was experimenting with file uploading possibility using ASP.NET.
I ran into a problem :(

I can upload small files, but if I try a bigger file it doesn't work...

Anyone who can help me please ?

Thanks.
K.
private void btnUpload_Click(object sender, System.EventArgs e)
{
string strBaseLocation = Server.MapPath( "./files" );

if ( txtUploadFile.Value == string.Empty )
{
lblInfo.Text = "Gelieve een bestand te selecteren.";
return;
}

string fileName = GetFileName( txtUploadFile.Value );
string fileTarget = strBaseLocation + "\\" + fileName;

if (null != txtUploadFile.PostedFile)
{
try
{
//read/write buffer
const int BUFFER_SIZE = 255;
Byte[] Buffer = new Byte[BUFFER_SIZE];
lblInfo.Text = "Uploading File: " + fileName;

//incoming external file stream
Stream theStream = txtUploadFile.PostedFile.InputStream;

//local file stream
FileStream fs = new FileStream(fileTarget, FileMode.CreateNew,
FileAccess.Write);

//local binary writer on local file stream
BinaryWriter bw = new BinaryWriter(fs);
// read/write loop
while( theStream.Read(Buffer, 0, BUFFER_SIZE) != 0 )
{
bw.Write(Buffer, 0, BUFFER_SIZE);
}

//Close the streams
bw.Close();
fs.Close();
theStream.Close();
lblInfo.Text = "Upload finished.";

//reload directory listing
FindAndDisplayFiles();
}
catch(Exception ex)
{
lblInfo.Text = "Fout bij het uploaden.<br>" + ex.Message;
}
}
}

Nov 19 '05 #2
Asshen Shugar wrote:
<httpRuntime maxRequestLength="4000"
How do I change it ?

You, uhh... change it. What happened when you tried?

Nov 19 '05 #3
I can upload files of a maximum size of 4000 kB.
That's the default maximum size specified in Machine.config.

I tried to add the <httpRuntime> node in the Web.config of my project, but
that doesn't work (guess machine.config has greater permission than on
application level ?).

So the only solution I see is changing the machine.config file ?
Is that correct ?

K

"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Asshen Shugar wrote:
<httpRuntime maxRequestLength="4000"
How do I change it ?

You, uhh... change it. What happened when you tried?

Nov 19 '05 #4
Asshen Shugar wrote:
I can upload files of a maximum size of 4000 kB.
That's the default maximum size specified in Machine.config.

I tried to add the <httpRuntime> node in the Web.config of my project, but that doesn't work (guess machine.config has greater permission than on application level ?).

So the only solution I see is changing the machine.config file ?
Is that correct ?

Strange. I ran into the same limitation recently, and the following
web.config fixed it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="600" />
</system.web>
</configuration>

Note that that's the entire web.config file for the directory that
needs it. I'd be hesitant to modify the web.config at the root of your
application, since you don't want to allow your users to routinely hit
you with 10mb requests.
Incedentally, the machine.config on our servers has the following line,
and the web.config above still allows large file downloads:

<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100"
enableVersionHeader="true"/>
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #5
Asshen Shugar wrote:
I can upload files of a maximum size of 4000 kB.
That's the default maximum size specified in Machine.config.

I tried to add the <httpRuntime> node in the Web.config of my project, but that doesn't work (guess machine.config has greater permission than on application level ?).

So the only solution I see is changing the machine.config file ?
Is that correct ?

Strange. I ran into the same limitation recently, and the following
web.config fixed it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="600" />
</system.web>
</configuration>

Note that that's the entire web.config file for the directory that
needs it. I'd be hesitant to modify the web.config at the root of your
application, since you don't want to allow your users to routinely hit
you with 10mb requests.
Incedentally, the machine.config on our servers has the following line,
and the web.config above still allows large file uploads:

<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100"
enableVersionHeader="true"/>
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #6
Ok, thanks.
I'll look into it tomorrow.

Greetings.
K.
"jasonkester" <ja*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Asshen Shugar wrote:
I can upload files of a maximum size of 4000 kB.
That's the default maximum size specified in Machine.config.

I tried to add the <httpRuntime> node in the Web.config of my

project, but
that doesn't work (guess machine.config has greater permission than

on
application level ?).

So the only solution I see is changing the machine.config file ?
Is that correct ?

Strange. I ran into the same limitation recently, and the following
web.config fixed it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="600" />
</system.web>
</configuration>

Note that that's the entire web.config file for the directory that
needs it. I'd be hesitant to modify the web.config at the root of your
application, since you don't want to allow your users to routinely hit
you with 10mb requests.
Incedentally, the machine.config on our servers has the following line,
and the web.config above still allows large file uploads:

<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100"
enableVersionHeader="true"/>
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #7
I have an extra question...
Is it possible to find the filesize of the file that a client wants to
upload before it is getting uploaded ?

K.


"Asshen Shugar" <va********@hotmail.com> wrote in message
news:6H*********************@phobos.telenet-ops.be...
Hey.

I was experimenting with file uploading possibility using ASP.NET.
I ran into a problem :(

I can upload small files, but if I try a bigger file it doesn't work...

Anyone who can help me please ?

Thanks.
K.
private void btnUpload_Click(object sender, System.EventArgs e)
{
string strBaseLocation = Server.MapPath( "./files" );

if ( txtUploadFile.Value == string.Empty )
{
lblInfo.Text = "Gelieve een bestand te selecteren.";
return;
}

string fileName = GetFileName( txtUploadFile.Value );
string fileTarget = strBaseLocation + "\\" + fileName;

if (null != txtUploadFile.PostedFile)
{
try
{
//read/write buffer
const int BUFFER_SIZE = 255;
Byte[] Buffer = new Byte[BUFFER_SIZE];
lblInfo.Text = "Uploading File: " + fileName;

//incoming external file stream
Stream theStream = txtUploadFile.PostedFile.InputStream;

//local file stream
FileStream fs = new FileStream(fileTarget, FileMode.CreateNew,
FileAccess.Write);

//local binary writer on local file stream
BinaryWriter bw = new BinaryWriter(fs);
// read/write loop
while( theStream.Read(Buffer, 0, BUFFER_SIZE) != 0 )
{
bw.Write(Buffer, 0, BUFFER_SIZE);
}

//Close the streams
bw.Close();
fs.Close();
theStream.Close();
lblInfo.Text = "Upload finished.";

//reload directory listing
FindAndDisplayFiles();
}
catch(Exception ex)
{
lblInfo.Text = "Fout bij het uploaden.<br>" + ex.Message;
}
}
}

Nov 19 '05 #8
Asshen what you can also do is to setup your web.config for the specific
folders.
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #9
dgk
On Wed, 23 Mar 2005 17:28:34 GMT, "Asshen Shugar"
<va********@hotmail.com> wrote:

I can't help you on the answer but how many people pick up on the
Valheru reference? One of my all time favorite stories.
Nov 19 '05 #10

Raymond Feist fan ?
Hehe, yeah great books aren't it :)

K.
"dgk" <so******************@hot-nospamp-mail.com> wrote in message
news:i4********************************@4ax.com...
On Wed, 23 Mar 2005 17:28:34 GMT, "Asshen Shugar"
<va********@hotmail.com> wrote:

I can't help you on the answer but how many people pick up on the
Valheru reference? One of my all time favorite stories.

Nov 19 '05 #11
Asshen Shugar wrote:
I have an extra question...
Is it possible to find the filesize of the file that a client wants to upload before it is getting uploaded ?


Careful. You're getting into the realm where we're going to start
asking 'What did you Try?' and shut down on you. If you typed
"myFileInputControl." and hit ctrl-space, you'd quickly find
PostedFile.ContentLength.

That will give you the content length of whatever your user is trying
to send you. If you like it, you can hook a fileStream up to it and
suck it down. If you don't, you can boot him to an error page with no
bandwidth wasted at your end.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #12
I tried that already.
Seems like I can't get the file size (ContentLength) until it is already
being uploaded (and get the resulting error page) !

Trust me, I did my research before asking the question, just didn't find a
working answer, hence the question here.

K.
"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Asshen Shugar wrote:
I have an extra question...
Is it possible to find the filesize of the file that a client wants

to
upload before it is getting uploaded ?


Careful. You're getting into the realm where we're going to start
asking 'What did you Try?' and shut down on you. If you typed
"myFileInputControl." and hit ctrl-space, you'd quickly find
PostedFile.ContentLength.

That will give you the content length of whatever your user is trying
to send you. If you like it, you can hook a fileStream up to it and
suck it down. If you don't, you can boot him to an error page with no
bandwidth wasted at your end.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #13
From MSDN:
Gets the size in bytes of an uploaded file.

[Visual Basic]
Public ReadOnly Property ContentLength As IntegerSo, it gets the file size
AFTER upload.

K.
"Asshen Shugar" <va********@hotmail.com> wrote in message
news:hl*********************@phobos.telenet-ops.be...
I tried that already.
Seems like I can't get the file size (ContentLength) until it is already
being uploaded (and get the resulting error page) !

Trust me, I did my research before asking the question, just didn't find a
working answer, hence the question here.

K.
"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Asshen Shugar wrote:
I have an extra question...
Is it possible to find the filesize of the file that a client wants

to
upload before it is getting uploaded ?


Careful. You're getting into the realm where we're going to start
asking 'What did you Try?' and shut down on you. If you typed
"myFileInputControl." and hit ctrl-space, you'd quickly find
PostedFile.ContentLength.

That will give you the content length of whatever your user is trying
to send you. If you like it, you can hook a fileStream up to it and
suck it down. If you don't, you can boot him to an error page with no
bandwidth wasted at your end.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/


Nov 19 '05 #14
Asshen Shugar wrote:
I tried that already.
Seems like I can't get the file size (ContentLength) until it is already being uploaded (and get the resulting error page) !


That is exactly true. So your solution is to use the web.config
setting to open yourself up to the maximum size you'll reasonably
expect your users to want to send you, then use ContentLength to reject
files larger than you actually want to receive.

But at the end of the day, there's really no way to give out a pretty
error message to a user that tries to upload a 40GB file.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #15
Ok, thank you very much.
I "investigated" this technique out of curiosity, but so on a professionel
level it would be better to advise some component I guess ?

K.

"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Asshen Shugar wrote:
I tried that already.
Seems like I can't get the file size (ContentLength) until it is

already
being uploaded (and get the resulting error page) !


That is exactly true. So your solution is to use the web.config
setting to open yourself up to the maximum size you'll reasonably
expect your users to want to send you, then use ContentLength to reject
files larger than you actually want to receive.

But at the end of the day, there's really no way to give out a pretty
error message to a user that tries to upload a 40GB file.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by matt | last post: by
3 posts views Thread by Bijoy Naick | last post: by
6 posts views Thread by tshad | last post: by
5 posts views Thread by William LaMartin | last post: by
6 posts views Thread by Vic Spainhower | last post: by
7 posts views Thread by pbd22 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.