第一次后文件下载变慢

时间:2017-06-13 21:04:09

标签: c# asp.net asp.net-mvc asp.net-mvc-5

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

我使用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 

2 个答案:

答案 0 :(得分:0)

我认为您应该在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();
        }
    }
}

这将确保正确关闭文件流

答案 1 :(得分:0)

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