相关疑难解决方法(0)

填充日志:与远程主机通信时发生错误

我们正在尝试了解此错误发生了什么,这只是用户与网站断开连接吗?

System.Web.HttpException  An error occurred while communicating with the remote host. The error code is 0x800703E3.  
System.Runtime.InteropServices.COMException  The I/O operation has been aborted because of either a thread exit or an application request. 
(Exception from HRESULT: 0x800703E3)  StackTrace:    
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)     
at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()    
at System.Web.HttpResponse.Flush(Boolean finalFlush)     
at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)     
at System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response)     
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c <InvokeActionResultWithFilters>b__19()     
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)    
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)     
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) …
Run Code Online (Sandbox Code Playgroud)

iis-7

8
推荐指数
1
解决办法
1万
查看次数

从静态方法调用Response.TransmitFile()

我有许多页面需要支持将数据导出到Excel电子表格.我可以很好地生成Excel文件,但我正在尝试弄清楚如何抽象这种行为,以便它可以从我需要的所有页面轻松地重用.我目前的想法是使用静态实用方法,如下所示:

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);  
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在调用的单击处理程序SendExcelFile如下所示:

protected void lnkExport_Click(object sender, EventArgs e)
{
    List<List<string>> dataList = GatherDataForSpreadsheet();
    Utility.SendExcelFile(this, "fileNameForDownload.xlsx", dataList, "MyReports");
}
Run Code Online (Sandbox Code Playgroud)

这段代码可以很好地作为调用页面的实例方法.但是,作为静态方法,它根本不起作用.当我单击调用此按钮时,浏览器会无限期地显示加载动画,但从不提示文件下载.

我对ASP.NET(以及一般的Web编程)很新,所以我确定我在这里遗漏了一些东西.有人可以解释一下我所看到的行为,并建议一种合理的替代方法吗?

编辑:如果我最后删除对File.Delete()的调用,该方法按预期工作.Response.TransmitFile()是否异步进行传输?

编辑2:我只需要在删除文件之前调用Response.Flush().请参阅下面的答案.谢谢!

c# asp.net response

3
推荐指数
1
解决办法
6877
查看次数

标签 统计

asp.net ×1

c# ×1

iis-7 ×1

response ×1