4

私は次のファイルアップロードハンドラーを持っています:

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が発生します。誰かがそのようなハンドラーを使用して大きなファイルをアップロードする正しい方法を教えてもらえますか?

4

2 に答える 2

7

どこかに書き込むためにファイルをメモリにロードする必要はありません。小さなバッファー(おそらく8k)を使用し、ストリームをループする必要があります。または、4.0では、このCopyTo方法。例えば:

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

(これは、デフォルトで4kバッファーを使用するか、カスタムバッファーサイズをオーバーロード経由で渡すことを可能にする、小さなバッファー/ループを実行します)

于 2012-05-29T08:41:20.610 に答える
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を削除しました

于 2012-05-29T08:44:48.303 に答える