从HTTPS

时间:2015-06-16 21:42:14

标签: c# httpresponse file-transfer

我有一个奇怪的情况。我编写的代码使用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

1 个答案:

答案 0 :(得分:2)

我会删除那些应该没用的ClearContent,Clear,Flush和Close。

内容类型也不正确,因为它是一个xls文件,它应该是“application / vnd.ms-excel”。

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