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