FileHandler http://www.cnblogs.com/vipsoft/p/3627709.html
UpdatePanel無法導出下載文件: http://www.cnblogs.com/vipsoft/p/3298299.html
//相對路徑下載。path: ~/DownLoad/ //<add key="DownLoadPath" value="~/DownLoad/"/> public static bool DownLoadFile(string path, string fileName) { bool result = false; try { string filePath = HttpContext.Current.Server.MapPath(path + fileName); if (File.Exists(filePath)) { FileInfo fileInfo = new FileInfo(filePath); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); HttpContext.Current.Response.WriteFile(fileInfo.FullName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); result = true; } } catch { } return result; } //物理路徑下載。path: D:\DownLoad\ //<add key="DownLoadPath" value="D:\DownLoad\"/> public static bool DownLoadFile(string path, string fileName) { bool result = false; string filePath = path + fileName; if (File.Exists(filePath)) { try { //string filePath = HttpContext.Current.Server.MapPath(path + fileName); FileInfo fileInfo = new FileInfo(filePath); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); HttpContext.Current.Response.WriteFile(fileInfo.FullName); HttpContext.Current.Response.Flush(); result = true; } catch (Exception e) { } finally { HttpContext.Current.Response.End(); //解決 ThreadAbortException 異常問題 } } return result; }
兩種方法的結合
public static bool DownLoadFile(string path, string fileName) { bool result = false; string filePath = path + fileName; if (File.Exists(filePath)) { result = true; } else { try { filePath = HttpContext.Current.Server.MapPath(path + fileName); if (File.Exists(filePath)) { result = true; } } catch { result = false; } } if (result) { try { //string filePath = HttpContext.Current.Server.MapPath(path + fileName); FileInfo fileInfo = new FileInfo(filePath); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); HttpContext.Current.Response.WriteFile(fileInfo.FullName); HttpContext.Current.Response.Flush(); } catch (Exception e) { } finally { HttpContext.Current.Response.End(); } } return result; }
根據一些業務邏輯返回相應的狀態字符串,如果出現異常做返回“error”,我預期它返回“狀態1”,結果測試時發現
AJAX回調的結果是“狀態1error”,它居然拋出異常了!
google后得知:Response.End 方法終止頁的執行,並將此執行切換到應用程序的事件管線中的
Application_EndRequest 事件,同時拋出ThreadAbortException 異常,異常信息為“正在中止線程”。另外
Response.Redirect、Server.Transfer方法也會出現這個問題,因為它們內部調用了Response.End 方法。
它給出的解決方案是使用HttpContext.Current.ApplicationInstance.CompleteRequest 方法以跳過
Application_EndRequest 事件的代碼執行,但是我試了后發現雖然不拋出異常了,但是頁面后面的代碼依然會執行,
達不到Response.End的效果。
Response.End導致“正在中止線程”異常的問題,來源:
http://www.cnblogs.com/jintianhu/archive/2011/02/16/1952833.html