首次下载后文件下载速度变慢



第一次调用此方法时,文件会立即下载,但是每次请求下载文件时,文件的时间都会越来越长。我不知道为什么。

我正在使用 DocX 库替换 Word 文档中的字符串,然后保存此文件。

保存后,我阅读文件并下载如下:

public void Download(int){
var model = new ApplicantNotificationViewModel();
var fileName = "";
var filePath = "";                
//business address
var businessAddressModel = (from aps in db.AppSettings
                            select new {
                            BusinessAddress1 = aps.BusinessAddress1,
                            BusinessAddress2 = aps.BusinessAddress2,
                            BusinessAddress3 = aps.BusinessAddress3
                            }).FirstOrDefault();
            // Load a .docx file
            using (DocX document = DocX.Load(AppDomain.CurrentDomain.BaseDirectory + "\files\template.docx"))
            {
                /*
                 * Replace each instance of the string pear with the string banana.
                 * Specifying true as the third argument informs DocX to track the
                 * changes made by this replace. The fourth argument tells DocX to
                 * ignore case when matching the string pear.
                 * document.ReplaceText("pear", banana, true, RegexOptions.IgnoreCase);*/
                //business address
                document.ReplaceText("BusinessAddress1", businessAddressModel.BusinessAddress1, false, RegexOptions.IgnoreCase);
                document.ReplaceText("BusinessAddress2", businessAddressModel.BusinessAddress2, false, RegexOptions.IgnoreCase);
                document.ReplaceText("BusinessAddress3", businessAddressModel.BusinessAddress3, false, RegexOptions.IgnoreCase);
            string fileSavePath = "SomePath";
                // Save changes made to this document
                fileName = "foo";
                filePath = fileSavePath + fileName + ".docx";
                document.SaveAs(filePath);
            }// Release this document from memory.
        //open saved doc
        using (var fs = System.IO.File.Open(filePath, FileMode.Open))
        {
            //open saved doc
            using (MemoryStream ms = new MemoryStream())
            {
                fs.CopyTo(ms);
                Response.AddHeader("content-disposition",
                  "attachment;" + "filename=" + fileName + ".docx");
                Response.OutputStream.Write(ms.GetBuffer(), 0,
                     ms.GetBuffer().Length);
                Response.End();
            }
        }
}

更新:

分解代码后,请求似乎在第一次文件下载后需要很长时间才能到达 mvc 控制器

IIS 日志

2017-06-13 21:42:45 ::1 GET / 200 0 0 94
2017-06-13 21:43:55 ::1 GET /Applicant/Download applicantid=1&templateid=1 
2017-06-13 21:44:19 ::1 GET /Applicant/Download applicantid=11&templateid=2 
2017-06-13 21:45:13 ::1 GET /Applicant/Download applicantid=11&templateid=2 
我认为

你也应该在你的FileStream周围使用 using-语句,如下所示:

public void DownloadFile()
{
    using (var fs = System.IO.File.Open(filePath, FileMode.Open)) {
         //open saved doc
         using (MemoryStream ms = new MemoryStream())
         {
            fs.CopyTo(ms);
            Response.AddHeader("content-disposition",
              "attachment;" + "filename=" + fileName + ".docx");
            Response.OutputStream.Write(ms.GetBuffer(), 0, 
                 ms.GetBuffer().Length);
            ms.Flush();
        }
    }
}

这将确保文件流正确关闭

好的,所以我解决了这个问题,感谢您的评论和建议。基本上是因为第一个文件是在 using 语句结束后使用 DocX 库加载的,所以它仍在使用中,不会在内存中释放。我还更改了方法 DocX.Load(( 以使用第二个重载,它接受一个流对象(取自 Steven Lemmens(。该文件也保存在解决方案bin文件夹中,我根据此链接将路径更改为另一个目标 forums.iis.net/t/1209378.aspx

最新更新