加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

利用repeater绑定下载地址并点击下载(避免中文文件名乱码)

发布时间:2020-12-17 01:05:01 所属栏目:安全 来源:网络整理
导读:? aspx ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? asp:Repeater ID="Repeater1" runat="server" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HeaderTemplate ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? table ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读