控制器:
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
....
当我在调试模式下查看流时,它显示它可以读取 = true、seek = true、lenght = 903234 等。
但我不断收到: 无法访问已关闭的文件
当我在本地/调试模式(VS IIS)运行 mvc 站点时,这工作正常,而在“RELEASE”模式下(当站点发布到 iis 时)不起作用。
我究竟做错了什么?