ASP.NET File Upload problem

A

Asshen Shugar

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;
}
}
}
 
A

Asshen Shugar

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.
 
A

Asshen Shugar

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
 
J

jasonkester

Asshen said:
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/
 
J

jasonkester

Asshen said:
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/
 
A

Asshen Shugar

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.
 
P

Patrick Olurotimi Ige

Asshen what you can also do is to setup your web.config for the specific
folders.
Patrick
 
D

dgk

On Wed, 23 Mar 2005 17:28:34 GMT, "Asshen Shugar"

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.
 
J

jasonkester

Asshen said:
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/
 
A

Asshen Shugar

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.
 
A

Asshen Shugar

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.
 
J

jasonkester

Asshen said:
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/
 
A

Asshen Shugar

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.
 
Joined
Mar 16, 2007
Messages
1
Reaction score
0
Have a problem when i upload a file using a web application

hi,
I have an application where i upload a file from my harddisk into the database thru a java applet. when i finish the upload i am not able to delete the file which i uploaded from my harddisk until i close the browser.

how do i get to delete the file without closing the browser as i cannot stop in between using my application as i would loose all the work done till then.

Regards,

Rajesh
 

vdk

Joined
Nov 20, 2008
Messages
3
Reaction score
0
Hi,
To upload the larger files through the upload control you need to add the

<httpRuntime
executionTimeout="90"
maxRequestLength="20096"
>
</httpRuntime>

in the web.config file for the application as the machine.config will allow you to keep the maxRequestlength="4096"
i.e just 4mb files
as mentioned above you can chge it to 20 mb as i did.
This sholud work for file sizes more than 4 mb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top