本文介绍了无法转换类型'System.Web.HttpInputStream'的对象类型“System.IO.FileStream”MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇见了关于从HttpInputStream到的FileStream铸造类型的问题。

I have met an issue regarding the casting type from HttpInputStream to FileStream.

我怎么做?

我有一个 HttpPostedFileBase 对象,我想拥有的FileStream。

I have a HttpPostedFileBase object and I want to have FileStream.

我说:

public void Test(HttpPostedFileBase postedFile) {
  FileStream fileStream = (FileStream)(postedFile.InputStream); // throw exception

  FileStream anotherFileStream = postedFile.InputStream as FileStream; // null
}

我也试过

public void Test(HttpPostedFileBase postedFile) {
  Stream stream = postedFile.InputStream as Stream;

  FileStream myFile = (FileStream)stream;

}

但没有成功。

为什么在 postedFile.InputStream 自带 HttpInputStream 键入?

和我怎么能解决这个问题呢?

And how could I solve this issue ?

感谢

推荐答案

这是你从你的HTTP调用得到的流是只读的顺序(非可搜索),并因此FileStream读/写可搜索。您需要首先从HTTP调用整个流读入一个字节数组,然后创建从阵列中的FileStream。

The stream that you get from your HTTP call is read-only sequential (non-seekable) and the FileStream is read/write seekable. You will need first to read the entire stream from the HTTP call into a byte array, then create the FileStream from that array.

这篇关于无法转换类型'System.Web.HttpInputStream'的对象类型“System.IO.FileStream”MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:27