从调用静态方法Response.TransmitFile() [英] Calling Response.TransmitFile() from static method
问题描述
我有许多这就需要导出数据支持到Excel US preadsheet页面。我可以生成Excel文件就好了,但我试图找出如何抽象这种行为所以它的所有的地方,我需要它的网页很容易重用。我现在的想法是使用一个静态的实用方法,如下所示:
I have a number of pages which need to support exporting data to an Excel spreadsheet. I can generate the Excel files just fine, but I'm trying to work out how to abstract this behavior so it's easily reusable from all of the pages where I need it. My current idea is to use a static utility method, as follows:
public static void SendExcelFile(System.Web.UI.Page callingPage, string downloadFileName, List<List<string>> data, string worksheetTitle)
{
string tempFileName = Path.GetTempFileName();
try
{
// Generate file using ExcelPackage
GenerateExcelDoc(tempFileName, data, worksheetTitle);
callingPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName);
callingPage.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
callingPage.Response.AddHeader("Content-Length", new FileInfo(tempFileName).Length.ToString());
callingPage.Response.TransmitFile(tempFileName);
}
finally
{
//When this is removed, the method works as expected.
if (File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
在这里我打电话单击处理 SendExcelFile
是这样的:
protected void lnkExport_Click(object sender, EventArgs e)
{
List<List<string>> dataList = GatherDataForSpreadsheet();
Utility.SendExcelFile(this, "fileNameForDownload.xlsx", dataList, "MyReports");
}
这code的作品就好作为调用页面的一个实例方法。作为一个静态方法,但是,它根本不工作。当我点击调用这个按钮,浏览器显示加载动画无限期的,但从来没有提示输入文件下载。
This code works just fine as an instance method of the calling page. As a static method, though, it doesn't work at all. When I click the button that invokes this, the browser shows the loading animations indefinitely, but never prompts for a file download.
我很新的ASP.NET(和一般的web编程),所以我敢肯定,我在这里失去了一些东西。可能有人请解释一下我看到的行为,并提出一个合理的选择这种方法?
I'm very new to ASP.NET (and web programming in general), so I'm sure I'm missing something here. Could someone please explain the behavior I'm seeing, and suggest a reasonable alternative to this approach?
编辑:如果我删除调用File.Delete()末,该方法按预期工作。是否Response.TransmitFile()做传输异步?
If I remove the call to File.Delete() at the end, the method works as expected. Does Response.TransmitFile() do the transfer asynchronously?
编辑2:我只需要调用Response.Flush()之前,我删除的文件。见我的回答如下。
谢谢!
EDIT 2: I just needed to call Response.Flush() before I deleted the file. See my answer below. Thanks!
推荐答案
的问题是,在数据被向下发送的临时文件被删除。我只需要调用Response.Flush()像这样:
The problem was that the temp file was being deleted before the data was sent down. I just needed to call Response.Flush() like so:
public static void SendExcelFile(System.Web.UI.Page callingPage, string downloadFileName, List<List<string>> data, string worksheetTitle)
{
string tempFileName = Path.GetTempFileName();
try
{
// Generate file using ExcelPackage
GenerateExcelDoc(tempFileName, data, worksheetTitle);
callingPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName);
callingPage.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
callingPage.Response.AddHeader("Content-Length", new FileInfo(tempFileName).Length.ToString());
callingPage.Response.TransmitFile(tempFileName);
callingPage.Response.Flush(); //This is what I needed
}
finally
{
if (File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
这篇关于从调用静态方法Response.TransmitFile()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!