C#实现文件下载
- 问题描述
在Ext环境下无法使用传统的.net方式实现文件下载,要么点了下载按钮无反应要么出现乱码。
- 原因分析
- 解决方案
-
- 主页面
-
-
- 方法一(JS代码)
-
window.location.href= "~/Service/DownLoad.aspx?filename="+ fileName +"&filepath="+ filePath; |
-
-
- 方法二(C#代码)
-
X.Redirect("~/Service/DownLoad.aspx?filename="+ fileName +"&filepath="+ filePath);
|
-
- 下载功能页面
publicpartialclassService_DownLoad: System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfilename = Request.QueryString["filename"];
stringfilepath = Request.QueryString["filepath"];
DownloadFile(filename, filepath);
}
///<summary>
///文件下载
///</summary>
///<param name="filename">文件名</param>
///<param name="filepath">文件路径 </param>
protectedvoidDownloadFile(stringfilename,stringfilepath)
{
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();
}
}
|