private readonly Dictionary<string, Stream> streams;
public ActionResult Upload(string qqfile, string id)
{
string filename;
try
{
Stream stream = this.Request.InputStream;
if (this.Request.Files.Count > 0)
{
// IE
HttpPostedFileBase postedFile = this.Request.Files[0];
stream = postedFile.InputStream;
}
else
{
stream = this.Request.InputStream;
}
filename = this.packageRepository.AddStream(stream, qqfile);
}
catch (Exception ex)
{
return this.Json(new { success = false, message = ex.Message }, "text/html");
}
return this.Json(new { success = true, qqfile, filename }, "text/html");
}
方法添加流:
public string AddStream(Stream stream, string filename)
{
if (string.IsNullOrEmpty(filename))
{
return null;
}
string fileExt = Path.GetExtension(filename).ToLower();
string fileName = Guid.NewGuid().ToString();
this.streams.Add(fileName, stream);
}
我想读一个二进制流,像这样:
Stream stream;
if (!this.streams.TryGetValue(key, out stream))
{
return false;
}
private const int BufferSize = 2097152;
using (var binaryReader = new BinaryReader(stream))
{
int offset = 0;
binaryReader.BaseStream.Position = 0;
byte[] fileBuffer = binaryReader.ReadBytes(BufferSize); // THIS IS THE LINE THAT FAILS
....
当我在调试模式下查看流,它显示它可以是read = true,seek = true,lenght = 903234等。
,但我不断收到: 无法访问已关闭的文件
,当我在本地运行MVC的网站/调试模式(VS IIS)这工作得很好,不会当“释放”模式(在网站就是工作发布到iis)。
我做错了什么?
请出示其中/ stream'是如何定义'。 – 2013-02-12 12:25:08
添加控制器和添加流方法 – ShaneKm 2013-02-12 12:32:29