Ext.Net 实现文件下载
- 问题描述
在Ext环境下无法使用传统的.net方式实现文件下载,要么点了下载按钮无反应要么出现乱码。
- 原因分析
- 解决方案
-
- 主页面
-
-
- 方法一(JS代码)
-
window.location.href= "~/Service/DownLoad.aspx?filename=" + fileName + "&filepath=" + filePath; |
-
-
- 方法二(C#代码)
-
X .Redirect("~/Service/DownLoad.aspx?filename=" + fileName + "&filepath=" + filePath);
|
-
- 下载功能页面
public partial class Service_DownLoad : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string filename = Request.QueryString["filename" ];
string filepath = Request.QueryString["filepath" ];
DownloadFile(filename, filepath);
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="filename"> 文件名</param>
/// <param name="filepath"> 文件路径 </param>
protected void DownloadFile( string filename, string filepath)
{
Response.Clear();
Response.AddHeader( "Content-Disposition" , "attachment; filename=" + HttpUtility .UrlEncode(filename, System.Text.Encoding .Default));
Response.ContentType = "application/octet-stream" ;
Response.TransmitFile(filepath);
Response.End();
}
}
|