troubleshooting Question

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

Avatar of plevintampabay
plevintampabay asked on
.NET ProgrammingC#ASP.NET
1 Comment1 Solution3356 ViewsLast Modified:
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();
ASKER CERTIFIED SOLUTION
plevintampabay

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros