Link to home
Create AccountLog in
Avatar of plevintampabay
plevintampabay

asked on

trying to upload "large" file with HttpWebRequest results in Request.Files.AllKeys.Length == 0 in web page

I have the code shown here, running in a C# Windows application.
This code will upload a file to a web page.
On the server, I have a simple ASP.NET web page that I wrote to get the uploaded file.  The lines in the web page that get the file are:

string f = Request.Files.AllKeys[0];
HttpPostedFile file = Request.Files[f];
file.SaveAs( file.FileName );

In the Web.Config file, I have the lines --

    <system.web>
      <httpRuntime maxRequestLength="102400" />
    </system.web>

This setting should allow files up to 100 MB to be uploaded.

When I upload a file that is 670 KB, it works fine.
But when I upload a file that is 20 MB, on the server,    Request.Files.AllKeys[0].Length  is 0, meaning that no files were uploaded.

There are no errors and no indication of what is wrong.

One more thing - the ASP.NET web page has a FileUpload control so I can test the web page from a browser.   Using FireFox, I can upload the 20 MB file just fine.  So the problem must be in the Windows application that is sending the file.

Any ideas how to fix this?
Thanks.


// construct the command url
string url = "http://mysite.com/myPage.aspx";
 
// get a unique string to use for the data boundary
string dataBoundary = Guid.NewGuid().ToString();
 
// get info about the file and open it for reading
FileInfo fileInfo = new FileInfo( filePath );
FileStream fs = new FileStream( filePath, FileMode.Open, FileAccess.Read );
 
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create( url );
webRequest.UserAgent = "mkfUpload";
webRequest.ContentType = "multipart/form-data; boundary=" + dataBoundary;
webRequest.Method = "POST";
webRequest.KeepAlive = true;
 
string headerStr = String.Format(
		  "--{0}\r\n"
		+ "Content-Disposition: form-data; name=\"File1\"; filename=\"{1}\"\r\n"
		+ "Content-Type: application/octet-stream\r\n"
		+ "\r\n",
		dataBoundary, fileInfo.Name );
byte[] headerBytes = Encoding.Default.GetBytes( headerStr );
 
string trailerStr = String.Format( "--{0}--\r\n", dataBoundary );
byte[] trailerBytes = Encoding.Default.GetBytes( trailerStr );
 
webRequest.ContentLength = headerBytes.Length + trailerBytes.Length + fileInfo.Length;
 
Stream webStream = webRequest.GetRequestStream();
 
// write the header to the web stream
webStream.Write( headerBytes, 0, headerBytes.Length );
 
// write the file to the stream
int size;
byte[] buf = new byte[1024 * 10];
do {
        size = fs.Read( buf, 0, buf.Length );
	if( size > 0 )
		webStream.Write( buf, 0, size );
} while( size > 0 );
 
// write the trailer to the stream
webStream.Write( trailerBytes, 0, trailerBytes.Length );
 
webStream.Close();
fs.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of plevintampabay
plevintampabay

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account