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; }
}