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

C#BinaryReader无法访问已关闭的文件,c#,asp.net,stream,binarystream,C#,Asp.net,Stream,Binarystream,控制器: private readonly Dictionary<string, Stream> streams; public ActionResult Upload(string qqfile, string id) { string filename; try { Stream stream = this.Request.InputStrea

控制器:

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");
        }
我正在尝试读取二进制流,如下所示:

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

但我一直得到: 无法访问已关闭的文件

当我在本地/调试模式(VS IIS)下运行mvc站点时,这可以正常工作,而在“发布”模式下(当站点发布到IIS时)则不起作用


我做错了什么?

您似乎依赖于您不控制的对象的生存期(HttpRequest对象的属性)。如果希望存储流的数据,则更安全的做法是立即将该数据复制到字节数组或类似数组中

您可以将AddStream更改为

    public string AddStream(Stream stream, string filename)
    {

        if (string.IsNullOrEmpty(filename))
        {
            return null;
        }

        string fileExt = Path.GetExtension(filename).ToLower();
        string fileName = Guid.NewGuid().ToString();
        var strLen = Convert.ToInt32(stream.Length);
        var strArr = new byte[strLen];
        stream.Read(strArr, 0, strLen);
        //you will need to change the type of streams acccordingly
        this.streams.Add(filename,strArr); 
    }
然后,当您需要流的数据时,您可以使用该数组,该数据使您能够完全控制对象的生存期。该数据存储在以下找到的解决方案中:

解决方案:

在生产环境中添加“requestLengthDiskThreshold”

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


请显示定义流的位置/方式。添加控制器和添加流方法Shane!我也遇到了这个错误,我应该在哪里添加这个?在web配置文件中?在生产环境中?谢谢:)在您的web.config文件中,当值为默认值时。如前所述,解析是显式设置RequestLengthDiskThreshold。此值是以字节还是以KB为单位?我将我的设置为65536,现在它工作正常,尽管我上传的文件总数约为179KB。。。即使微软的文档上说的是字节。
<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>