1

HttpPostedBaseFile のバイトを見て、アップロードされたコンテンツを確認したいと考えています。ただし、ストリームを開くと、データが消去されているように見えます

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

...
}

HttpPostedFileBase の InputStream を保存するか、保存されているように見せたいです。InputStream を保持しながら、多くのバイトを読み取る/覗くにはどうすればよいですか?


編集: 別のアプローチを取り、ストリーム データを読み取り、メタデータを poco にストリーミングしました。次に、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; }
}
4

1 に答える 1

2

をシークすることはできませんNetworkStream。一度読んだら、なくなります。

これを行う必要がある場合は、 を作成し、MemoryStreamを使用Stream.CopyToして内容をそれにコピーします。その後、メモリ ストリームを使用して好きなことを行うことができます。

于 2014-01-26T10:58:19.933 に答える