Question

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");
        }

method for adding stream:

        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);
        }

I'm trying read a binary stream like so:

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

When I view stream in debug mode it shows that it can be read = true, seek = true, lenght = 903234 etc.

but I keep getting: Cannot access a closed file

This works fine when I run mvc site locally/debug mode (VS IIS) and DOES NOT WORK when in "RELEASE" mode (when site is published to iis).

what am I doing wrong?

Was it helpful?

Solution

Found solution here:

uploading file exception

Solution:

add "requestLengthDiskThreshold" on production envirenment

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

OTHER TIPS

It would seem you are relying on the lifetime of an object you do not control (properties of the HttpRequest object). If you wish to store the data of the stream it would be safer to copy that data immediately to a byte array or similar

you could change AddStream to

    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); 
    }

you can then use the array when you need the data of the stream which gives you complete control of the lifetime of the object the data is stored in

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top