2014-01-26 4 views
1

업로드 된 콘텐츠를보기 위해 HttpPostedBaseFile의 바이트를보고 싶습니다. 그래도 난 스트림을 열 때, 데이터 I는 HttpPostedFileBase의의 InputStream을 보존하거나 보존되었음을 모양을주고 싶은 버퍼를 지우지 않고 httppostedbase 메모리 스트림을 엿보기

private bool IsAWordDocument(HttpPostedFileBase httpPostedFileBase) 
{ 

    .... 
    byte[] contents = null; 
    using (var binaryReader = new BinaryReader(httpPostedFileBase.InputStream)) 
    { 
     contents = binaryReader.ReadBytes(10); 
     //binaryReader.BaseStream.Position = 0; 
    } 

    //InputStream is empty when I get to here! 
    var properBytes = contents.Take(8).SequenceEqual(DOC) || contents.Take(4).SequenceEqual(ZIP_DOCX); 
    httpPostedFileBase.InputStream.Position = 0; //reset stream position 

... 
} 

을 취소 나타납니다. 어떻게하면 InputStream을 보존하면서 많은 바이트를 읽거나 엿볼 수 있습니까?

편집

: 나는 다른 접근 방식을 데리고 스트림 데이터를 읽고, 그리고 마시고에 메타 데이터를 스트리밍. 그런 다음 POCO를 지나쳤습니다.

public class FileData 
{ 
    public FileData(HttpPostedFileBase file) 
    { 
     ContentLength = file.ContentLength; 
     ContentType = file.ContentType; 
     FileExtension = Path.GetExtension(file.FileName); 
     FileName = Path.GetFileName(file.FileName); 

     using (var binaryReader = new BinaryReader(file.InputStream)) 
     { 
      Contents = binaryReader.ReadBytes(file.ContentLength); 
     } 

    } 
    public string FileName { get; set; } 
    public string FileExtension { get; set; } 
    public string ContentType { get; set; } 
    public int ContentLength { get; set; } 
    public byte[] Contents { get; set; } 
} 

답변

2

NetworkStream을 찾을 수 없습니다. 일단 당신이 그것을 읽으면, 그것은 없어집니다.

이 작업을 수행해야하는 경우 MemoryStream을 만들고 Stream.CopyTo을 사용하여 내용을 복사하십시오. 그런 다음 메모리 스트림으로 원하는 모든 것을 할 수 있습니다.

관련 문제