利用repeater绑定下载地址并点击下载(避免中文文件名乱码)
2012-01-20 14:39
302 查看
aspx
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border: 0px;">
<a href='../download.ashx?url=<%# Server.UrlEncode(Eval
("FilePath").ToString())%>'>
<%#eval_r("FileName")%></a>
</td>
</ItemTemplate>
<FooterTemplate>
</tr></table></FooterTemplate>
</asp:Repeater>
download.ashx
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace Web.UCenter
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class download : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string path = context.Request.QueryString["url"].ToString();
System.IO.FileInfo file = new System.IO.FileInfo(path);
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
context.Response.AddHeader
("Content-Disposition",
"attachment; filename="
+
System.Web.HttpUtility.UrlEncode
(file.Name,System.Text.Encoding.UTF8));// 防止中文名有乱码
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
context.Response.AddHeader("Content-Length", file.Length.ToString());
//// 指定返回的是一个不能被客户端读取的流,必须被下载
//context.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
context.Response.WriteFile(file.FullName);
// 停止页面的执行
context.Response.End();
}
catch
{
context.Response.Write("您下载的资源不存在!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
注意点:我在数据库存储的文件路径是加server.map的绝对路径。实际下载的时候根据实际情况修改代码。
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border: 0px;">
<a href='../download.ashx?url=<%# Server.UrlEncode(Eval
("FilePath").ToString())%>'>
<%#eval_r("FileName")%></a>
</td>
</ItemTemplate>
<FooterTemplate>
</tr></table></FooterTemplate>
</asp:Repeater>
download.ashx
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace Web.UCenter
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class download : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string path = context.Request.QueryString["url"].ToString();
System.IO.FileInfo file = new System.IO.FileInfo(path);
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
context.Response.AddHeader
("Content-Disposition",
"attachment; filename="
+
System.Web.HttpUtility.UrlEncode
(file.Name,System.Text.Encoding.UTF8));// 防止中文名有乱码
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
context.Response.AddHeader("Content-Length", file.Length.ToString());
//// 指定返回的是一个不能被客户端读取的流,必须被下载
//context.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
context.Response.WriteFile(file.FullName);
// 停止页面的执行
context.Response.End();
}
catch
{
context.Response.Write("您下载的资源不存在!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
注意点:我在数据库存储的文件路径是加server.map的绝对路径。实际下载的时候根据实际情况修改代码。
相关文章推荐
- 利用org.apache.commons.net.ftp实现ftp下载,解决中文文件名乱码问题
- 下载方法(避免中文乱码和点击不下载)
- Java下载文件,文件名中文乱码问题
- ASP.NET实现下载时的中文文件名乱码处理
- response.setHeader()设置下载中文文件名乱码问题
- 下载文件默认文件名中文乱码问题的解决
- java下载中文文件名乱码的解决方法
- 打包下载文件 中文文件名乱码问题的解决
- 彻底解决Asp.net 文件下载时文件名的中文乱码和空格异常全球性技术难题
- JSP、Struts2下载中文文件名乱码问题
- 关于Struts2下载的文件名中文乱码
- [Java]response.setHeader()下载中文文件名乱码问题
- 不同浏览器上中文文件名的下载/导出乱码问题(Java)
- 用火狐浏览器下载中文文件名的文件乱码及其解决
- Struts2 文件下载中文文件名乱码问题
- struts2下载中文文件名乱码
- Ubuntu1系统中下载的中文文件名乱码解决办法
- linux下下载的zip文件解压之后中文文件名乱码
- java web应用文件下载(包括中文文件名乱码处理)
- 跨浏览器PHP下载文件名中的中文乱码问题解决方法