3

My code:

int chunk = Request["chunk"] != null ? int.Parse(Request["chunk"]) : 0;
string fileName = Request["name"] != null ? Request["name"] : string.Empty;
Response.Write(Request.Files.Count);
HttpPostedFile fileUpload = Request.Files[0];

var uploadPath = Server.MapPath("~/uploaded-files");
if (Request.QueryString["originals"] == "yes")
{
    using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
    {
        var buffer = new byte[fileUpload.InputStream.Length];
        fileUpload.InputStream.Read(buffer, 0, buffer.Length);
        fs.Write(buffer, 0, buffer.Length);
    }
}

I get an 'System.OutOfMemoryException' error.

My machine has 4 gigs of RAM. The file in question is approximately 1 GB. I am using plUpload to do the upload.

I've Enabled the 3GB switch. No difference. I have lots of RAM available, why am I running out of memory? Is there an alternative approach that uses less memory?

5
  • Read and write it in chunks instead of all at once? Commented Jul 5, 2013 at 18:03
  • Try compiling for x64 platform target. Commented Jul 5, 2013 at 18:09
  • Have you messed with your httpRuntime settings in config? HttpPostedFile should buffer to disk when over 256KB, unless you've upped requestLengthDiskThreshold. Commented Jul 5, 2013 at 18:12
  • <httpRuntime maxRequestLength="2000000" /> Commented Jul 5, 2013 at 18:14
  • I'm going to look into chunk uploading Commented Jul 5, 2013 at 18:15

1 Answer 1

4

An alternative implementation would be to use SaveAs on HttpPostedFile:

fileUpload.SaveAs(Path.Combine(uploadPath, fileName));
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! This does the trick, I'm still going to look into chunk uploading, but for now this works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.