FileStream 对象需要很长时间才能将文件推送到浏览器

FileStream Object taking forever to push file to browser

提问人:Levi Wallach 提问时间:6/12/2021 更新时间:6/12/2021 访问量:49

问:

我正在使用文件流对象从我们的数据库推送 excel 格式的报告,它适用于行数合理的报告,但是有一个报告有超过 15K 行,并且站点在 20 分钟后超时 - 我知道我可以增加超时,但现在即使是 20 分钟也无法适应。有什么方法可以加快速度吗?这是我目前的代码:

string path = Server.MapPath(@"~\");
                string sourceName = Path.Combine(path, "Demo", "Report_Template", templateName);

                string newFileName = Path.Combine(Global.demo_data_directory, @"Reports", @"Demo Download Activity Report.xlsx"); // Path.Combine(path, "Demo_Data", "Reports", Filename + ".xlsx");

                string copyresults = Utilities.CopyFile(sourceName, newFileName);
                if (copyresults != "Copied file")
                {
                    DemoDAL.ErrorLog("GenerateDownloadActivityReportFile.Page_Load: Copy Error - " + copyresults);
                }
                else
                {
                    document = SpreadsheetDocument.Open(newFileName, true);
                    wbPart = document.WorkbookPart;

                    DataTable datatbl = (DataTable)Session["gvPOC"];

                    CreateReport(datatbl);

                    //  ADDED THIS *********************************************************************************************************
                    //wbPart.Workbook.Save();
                    using (FileStream fsSource = new FileStream(newFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 10240))
                    {
                        // Read the source file into a byte array.
                        byte[] bytes = new byte[fsSource.Length];
                        int numBytesToRead = (int)fsSource.Length;
                        int numBytesRead = 0;
                        while (numBytesToRead > 0)
                        {
                            // Read may return anything from 0 to numBytesToRead.
                            int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                            // Break when the end of the file is reached.
                            if (n == 0)
                                break;

                            numBytesRead += n;
                            numBytesToRead -= n;
                        }
                        numBytesToRead = bytes.Length;

                        StreamFileToBrowser(newFileName, bytes);
                    }
                    File.Delete(newFileName);
                }

public void StreamFileToBrowser(string sfilename, byte[] fileBytes)
    {
        try
        {
            Response.Clear();
            Response.ClearHeaders();

            Response.AppendHeader("Content-disposition", String.Format("attachment; filename=\"{0}\"", System.IO.Path.GetFileName(sfilename)));
            Response.AppendHeader("Content-Type", "binary/octet-stream");
            Response.AppendHeader("Content-length", fileBytes.Length.ToString());

            Response.BinaryWrite(fileBytes);

            if (Response.IsClientConnected)
                Response.Flush();
        }
        catch (Exception ex)
        {
            DemoDAL.ErrorLog("StreamFileToBrowser: " + ex.Message);
        }
    }
asp.net filestream iis-8

评论

0赞 Theobald Du 6/14/2021
您可以参考:stackoverflow.com/questions/3222305/...
0赞 Levi Wallach 6/14/2021
我看到了,但这并不相关。此报告需要在任何给定时间动态生成,因为其中的数据会不断添加。

答: 暂无答案