ASP.NET 文件下载
来源:互联网 发布:软件开发项目甘特图 编辑:程序博客网 时间:2024/04/30 02:57
下载页面:
<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();
}
}
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:
http://blog.csdn.net/gdjlc/article/details/4733266
- 下载文件 asp.net
- ASP.NET文件下载
- asp.net 文件下载
- asp.net 下载文件
- Asp.net 下载文件
- asp.net文件下载
- Asp.net 文件下载
- ASP.NET 文件下载
- ASP.NET 下载文件
- asp.net下载文件
- Asp.Net 文件下载
- ASP.NET 文件下载
- ASP.NET下载文件
- ASP.NET下载文件
- asp.net下载文件
- ASP.NET文件下载
- asp.net下载文件
- ASP.NET 文件下载
- 查看端口被占用命令
- SQL Server 学习笔记 第一篇
- 使用ffmpeg采集摄像头的视频和语音,合成mpg文件
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- iOS翻页(PageCurl)效果总结
- ASP.NET 文件下载
- 总结几种C#窗体间通讯的处理方法
- 数据库设计原则1
- (转)matlab数据类型及其简单的使用方法
- sharepoint 中获取用的 控件
- SQL中几种截取字符串方法的比較
- SSH remote connect error
- Silverlight客户端和WCF服务器端共享类库
- sqlite自增:ROWID