ASP.NET 文件下载
2011-07-21 16:25
141 查看
下载页面: <a href="download.ashx?url=<%=Server.UrlEncode("说明.txt")%>">下载</a>------------------------------------------------------------------------------ download.ashx code: <%@ WebHandler Language="C#" Class="download" %>
using System;
using System.Web;
public class download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
downloadfile(url);
}
public bool IsReusable {
get {
return false;
}
}
public void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
} From:/article/9522110.html
using System;
using System.Web;
public class download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
downloadfile(url);
}
public bool IsReusable {
get {
return false;
}
}
public void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
} From:/article/9522110.html
相关文章推荐
- 解决用ASP.NET下载文件时,文件名为乱码的问题
- ASP.net文件下载函数
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net MVC 处理文件的上传下载
- Asp.Net 之 下载文件的常用方式
- asp.net 文件上传下载相关资料汇集
- Asp.net 2.0 装载XML文件中数据到Excel文件中(示例代码下载)
- ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护(示例代码下载)
- ASP.NET : 如何将服务端的多个文件打包下载
- 黑马程序员_学习日记71_723ASP.NET(文件上传下载、IsPostBack原理、页面生命周期)
- ASP.NET多文件批量打包下载
- asp.net 下载文件几种方式
- ASP.NET中下载文件的实现
- Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转
- 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)
- asp.net 文件下载的通用方法
- iOS开发之结合asp.net webservice实现文件上传下载
- asp.net文件下载