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的绝对路径。实际下载的时候根据实际情况修改代码。