从 HTTPS 下载时缺少文件的最后 8 个字节

Missing last 8 bytes of file when downloading from HTTPS

我有一个奇怪的情况。我编写的一段代码使用 Excel Interop 创建一个 XLS 文件并将其保存到服务器。然后使用 System.Web.HttpResponse 传输此文件,然后删除。我的问题是,当我使用 HTTPS 而不是 HTTP 从面向外的演示站点下载此文件时,该文件将在此传输过程中损坏(在服务器上没问题),因为它丢失了最后 8 个字节的数据。如果我将协议更改为 HTTP,文件传输就很好。我错过了什么?这是执行此请求的方法的最后部分。

        #region Save & Quit
        Guid guid = Guid.NewGuid();

        //Save and quit, use SaveCopyAs since SaveAs does not always work
        string fileName = "IRSReport_" + guid.ToString() + ".xls";
        string target = Server.MapPath("~/" + fileName);

        xlApp.DisplayAlerts = false; //Supress overwrite request
        xlWorkBook.SaveAs(target, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        //Release objects
        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

        //Give the user the option to save the copy of the file anywhere they desire
        String FilePath = Server.MapPath("~/" + fileName);
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/plain";
        response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
        response.TransmitFile(FilePath);
        response.Flush();
        response.Close();

        //Delete the temporary file
        DeleteFile(fileName);
        #endregion

我会删除应该没用的 ClearContent、Clear、Flush 和 Close。

另外内容类型不对,因为是xls文件,应该是"application/vnd.ms-excel".

最后,我会尽量不要过早删除文件,让服务器在您删除文件之前发送文件。