gpt4 book ai didi

C# BinaryReader 无法访问已关闭的文件

转载 作者:太空狗 更新时间:2023-10-30 00:32:54 24 4
gpt4 key购买 nike

Controller :

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
....

当我在 Debug模式下查看流时,它显示它可以是 read = true、seek = true、lenght = 903234 等。

但我不断收到:无法访问已关闭的文件

当我在本地运行 mvc 站点/ Debug模式 (VS IIS) 时,这工作正常,但在“发布”模式下(当站点发布到 iis 时)不工作。

我做错了什么?

最佳答案

在这里找到解决方案:

uploading file exception

解决方法:

在生产环境中添加"requestLengthDiskThreshold"

<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>

关于C# BinaryReader 无法访问已关闭的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14832456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com