【问题标题】:File Download from Popup on ASPX Page Via generic Handler通过通用处理程序从 ASPX 页面上的弹出窗口下载文件
【发布时间】:2015-01-29 14:13:27
【问题描述】:

我的 aspx 页面有一个下载链接。单击它时会弹出一个窗口,要求用户输入一些数据,根据这些数据在下载之前修改文件。

我已将弹出窗口发出的请求转发给通用处理程序,该处理程序具有生成文件并将其附加到响应标头中的代码。

现在,我收到一个线程中止异常,因为由于代码已优化或本机框架位于调用堆栈顶部,因此无法计算表达式。

我猜响应头没有被客户端解释,因为弹出。 请找到代码 sn-p 并告诉我哪里出错了:

弹出按钮的 ASPX:

 <button id="BtnDownload" class="btnSubmit" onclick="javascript:downloadFileViaHandler()">
                                        Download</button>

Javascript 函数:

function downloadFileViaHandler() {
$('#popupdiv').dialog('close');
var urlHandler = "/WebHandlers/downloadHandler.ashx";
var urlHandlerParam = window.location.href.split('/');
var actualUrlparam = urlHandlerParam[0] + "/" + urlHandlerParam[1] + "/" + urlHandlerParam[2] + "/" + urlHandlerParam[3];
//Sanitize URLs for Server     
var urlHandler = actualUrlparam + urlHandler;
 $.ajax({
  type: 'POST',
  async: false,      
  url: urlHandler,
  data: { FileID: '' + FileID + '\'' },
  success: function (data) {
      console.info(data);
      },
  error: function () {
      alert("Error occured while Downloding File");
  }
  });

}

处理程序代码:

 public void ProcessRequest(HttpContext context)
 {  
   //some Logic to generate file//

 string filePath = str_SaveFileLocation;
 DownloadFilePath = (str_SaveFileLocation); 
 string name = Path.GetFileName(DownloadFilePath);
 string ext = Path.GetExtension(DownloadFilePath); 
 string Filetype = "text/DBC";
 context.Response.Clear();
 context.Response.AppendHeader("content-disposition", "Downloads;   filename=" + name);
 context.Response.ContentType = Filetype;
 context.Response.WriteFile(DownloadFilePath);
 context.Response.End();
}

【问题讨论】:

    标签: asp.net popup handler httpresponse generic-handler


    【解决方案1】:

    当您调用Response.End(). 时,ThreadAbortException 是正常的,请参阅documentation

    为了模仿 ASP 中 End 方法的行为,该方法试图 引发 [ThreadAbortException] 异常。如果这次尝试是 成功,调用线程将被中止,这是有害的 对您网站的性能。在这种情况下,调用后没有代码 End 方法被执行。如果 End 方法无法引发 [ThreadAbortException],而是将响应字节刷新到 客户。它同步执行此操作,这也可能对 您网站的性能。

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2015-02-06
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多