2016-10-24 2 views
0

파일의 일부만 웹 API에서 스트림으로 읽고 메모리에서 전체 파일을 가져 오지 않고이 스트림에서 작업을 수행하는 방법은 무엇입니까? NB : 읽기 전에 파일을 저장하고 싶지 않습니다. 웹 API 컨트롤러에 업로드됩니다.웹 API 2 - 파일 읽기 스트림

하지만 제가 정말하고 싶은 것은 구현 된 다음 의사 코드 :

foreach file in Request 
{ 
    using (var sr = new StreamReader(fileStream)) 
    { 
     string firstLine = sr.ReadLine() ?? ""; 
     if (firstLine contains the magic I need) 
     { 
      // would do something with this line, 
      // then scrap the stream and start reading the next file stream 
      continue; 
     } 
    } 
} 
+0

파일이 저장되지 않으면 해당 메모리를 의미합니다. 그렇지 않으면 파일 – Tinwor

+0

을 읽을 가능성이 없습니다. 컨트롤러를 게시 할 때 처음부터 메모리에 전체 파일이 있습니까? – user2330270

+0

클라이언트에서 스트리밍하지 않습니까? – user2330270

답변

0

여기로 : 업로드 된 파일을 다루는 스트리밍 모드로 http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/

당신이 할 수있는 "강제 웹 API, 오히려 전체 요청 입력 스트림을 메모리에 버퍼링하는 것보다 훨씬 효율적입니다. "

public class NoBufferPolicySelector : WebHostBufferPolicySelector 
{ 
    public override bool UseBufferedInputStream(object hostContext) 
    { 
     var context = hostContext as HttpContextBase; 

     if (context != null) 
     { 
     if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "uploading", StringComparison.InvariantCultureIgnoreCase)) 
      return false; 
     } 

     return true; 
    } 

    public override bool UseBufferedOutputStream(HttpResponseMessage response) 
    { 
     return base.UseBufferedOutputStream(response); 
    } 
} 

public interface IHostBufferPolicySelector 
{ 
    bool UseBufferedInputStream(object hostContext); 
    bool UseBufferedOutputStream(HttpResponseMessage response); 
} 

불행하게도 system.web에 크게 의존으로 포스트에서 언급 한대로 웹 API와 함께 주위에 얻을 수없는 것 같다.