ASP.NET C#大文件上传时出现OutOfMemoryException异常

5
我有以下文件上传处理程序:
public class FileUploader : IHttpHandler
{
 public void ProcessRequest(HttpContext context)
 {
    HttpRequest request = context.Request;

    context.Response.ContentType = "text/html";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    var tempPath = request.PhysicalApplicationPath + "\\Files\\TempFiles\\";        
    byte[] buffer = new byte[request.ContentLength];
    using (BinaryReader br = new BinaryReader(request.InputStream))
    {
        br.Read(buffer, 0, buffer.Length);
    }
    var tempName = WriteTempFile(buffer, tempPath);
    context.Response.Write("{\"success\":true}");
    context.Response.End();
 }

 public bool IsReusable
 {
    get { return true; }
 }

 private string WriteTempFile(byte[] buffer, string tempPath)
 {
    var fileName = GetUniqueFileName(tempPath);
    File.WriteAllBytes(tempPath + fileName, buffer);
    return fileName;
 }
 private string GetUniqueFileName(string tempPath)
 {
    var guid = Guid.NewGuid().ToString().ToUpper();
    while (File.Exists(tempPath + guid))
    {
        guid = Guid.NewGuid().ToString().ToUpper();
    }
    return guid;
 }
}

当我上传大文件时,会引发OutOfMemoryException异常。请问有什么正确的方法可以使用这样的处理程序上传大文件?

99%的情况下,使用BinaryReader/Writer都是非常错误的选择。 - leppie
有两个答案,它们执行相同的工作吗?如果不是,哪一个比另一个更好,为什么? - Babu James
2个回答

7

不需要将文件加载到内存中再写入到其他地方。应该使用一个缓冲区(可能为8k),并循环处理流。或者,在4.0中,可以使用CopyTo方法。例如:

using(var newFile = File.Create(tempPath)) {
    request.InputStream.CopyTo(newFile);
}

(默认情况下,它会为您执行小缓冲区/循环,使用4k缓冲区,或允许通过重载传递自定义缓冲区大小)

4

如果将上传的文件加载到内存中,可能会导致 OutOfMemoryException 异常。为了避免这种情况,应该直接将文件流写入文件。

public void ProcessRequest(HttpContext context)
 {
    const int BufferSize = 4096;    

    HttpRequest request = context.Request;

    context.Response.ContentType = "text/html";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    var tempFilePath = Path.GetTempFileName();        

    using (Stream fs = File.OpenWrite(tempFilePath));
    {
        byte[] buffer = new byte[BufferSize];
        int read = -1;
        while(read = request.InputStream.Read(buffer, 0, buffer.Length) > 0)
        {            
             fs.Write(buffer, 0, buffer.Length);             
        }
    }

    context.Response.Write("{\"success\":true}");
    context.Response.End();
 }

编辑:已删除BinaryReader


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接