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

Sha*_*eKm 4 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.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");
        }
Run Code Online (Sandbox Code Playgroud)

添加流的方法:

        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);
        }
Run Code Online (Sandbox Code Playgroud)

我正在尝试读取二进制流,如下所示:

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
    ....
Run Code Online (Sandbox Code Playgroud)

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

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

当我在本地/调试模式(VS IIS)运行mvc站点并且在"RELEASE"模式下(当站点发布到iis时)不工作时,这可以正常工作.

我究竟做错了什么?

Sha*_*eKm 8

找到解决方案:

上传文件例外

解:

在生产环境中添加"requestLengthDiskThreshold"

<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>
Run Code Online (Sandbox Code Playgroud)