2011-10-17 4 views
6

새로운 asp.net 4.5 비동기 처리기와 Request.GetBufferlessInputStream을 사용하여 디스크에 이미지 업로드를 작성하는 방법을 알아 내려고합니다. 이 코드가 실행되고 파일을 쓰지 만 이미지가 손상되어서 그 이유를 모르겠습니다. 여기에 내가 사용하고있는 코드가있다.NET 4.5 HttpTaskAsyncHandler 파일 업로드

public class UploadHandler : HttpTaskAsyncHandler 
{ 
    public override Task ProcessRequestAsync(HttpContext context) 
    { 
     // Gets a Stream object that can be used to read the 
     // incoming HTTP entity body, optionally disabling the 
     // request-length limit that is set in the MaxRequestLength property. 

     // This method provides an alternative to using the 
     // InputStream property. The InputStream property waits until the 
     // whole request has been received before it returns a Stream object. 
     // In contrast, the GetBufferlessInputStream method returns 
     // the Stream object immediately. 
     // You can use the method to begin processing the 
     // entity body before the complete contents of the 
     // body have been received. 
     // The entity body (or as much of it as you request and has 
     // been received) is returned only when you use the object that 
     // is returned by this method to read the stream, by calling 
     // methods such as the Read method. You use parameters of the 
     // Read method to specify how much of the entity body to read. 

     // This method can be useful if the request is uploading a 
     // large file and you want to begin accessing the file contents 
     // before the upload is finished. 
     // However, you should only use this method for scenarios where 
     // you want to take over all processing of the entity body. 
     // This means that you cannot use this method from an .aspx page, 
     // because by the time an .aspx page runs, the entity body 
     // has already been read. 

     using (Stream input = context.Request.GetBufferlessInputStream(true)) 
     using (var file = new FileStream("C:\\myfile.jpg", FileMode.Create, 
      FileAccess.Write, FileShare.Write)) 
     { 
      input.CopyTo(file); 
     } 

     context.Response.ContentType = "text/plain"; 
     return context.Response.Output.WriteAsync("Done"); 
    } 
} 
+1

어떻게 손상? – Amy

+0

위의 내용 외에 큰 텍스트 파일로 시도해보십시오. 그런 식으로 결과에 액세스 할 수 있지만 파일에 무슨 일이 일어나는지 확인할 수 있습니다. – stevenrcfox

+0

실제 요청을 구문 분석하고 양식/다중 파트 데이터를 찾아야한다고 생각합니다. – superlogical

답변

0

내가 실제로 알아 차리지 못했던 한 가지. Response.ContentType = image/gif 또한 스트림이 일반 스트림이 아닌 BinaryStream이되어서는 안되며, 그 이유는이 스트림이 작업중인 이미지이기 때문입니다.

+0

Response.ContentType은 text/plain이어야합니다. 브라우저에 응답으로 "Done"을 비동기 적으로 쓰고 있기 때문입니다. – superlogical