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的绝对路径。实际下载的时候根据实际情况修改代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。