Home > front end >  MemoryStream readtimeout during generate Excel file
MemoryStream readtimeout during generate Excel file

Time:10-15

I created an excel file. It saved correctly on the disk, but now I would like to download it via the browser. I have problem with stream. I have exception: ReadTimeout = 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

My code:

...
MemoryStream stream = new MemoryStream();
            using (ExcelPackage xlPackage = new ExcelPackage(stream))
            {
                ExcelWorksheet worksheet;
                worksheet = xlPackage.Workbook.Worksheets.Add(name);

                worksheet.Name = name;

                for (int i = 0; i < nameColumns.Length; i  )
                {
                    worksheet.Cells[1, i   1].Value = nameColumns[i];
                }
                xlPackage.Save();

            }
            stream.Position = 0;
            var contentType = "application/octet-stream";
            var fileName = "fileName.xlsx";
            return Ok(File(stream, contentType, fileName));
        }

screen with error

Many thanks for help. Regards

CodePudding user response:

Don't worry about it; you're seeing that because the debugger calls every property to get its value so it can show you the value on the tooltip, but those properties throw an exception on a MemoryStream because a MemoryStream cannot time out.

MemoryStream has to have those properties because all Streams have them (anything that has Stream as a parent class has these properties), but they don't make sense in the context of a memory stream.

You code doesn't access them when it actually runs so it's fine to ignore them

This is one thing to bear in mind for the future- the debugger accesses everything when you ask it to show you an object, which means it probably experience a lot of exceptions that your code never will, unless your code calls the same thing the debugger did (if your code had eg stream.ReadTineout = 10 it would also get an error)

  •  Tags:  
  • c#
  • Related