博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET 文件下载
阅读量:2119 次
发布时间:2019-04-30

本文共 1683 字,大约阅读时间需要 5 分钟。

下载页面:
 <a href="download.ashx?url=<%=Server.UrlEncode("说明.txt")%>">下载</a>
------------------------------------------------------------------------------
download.ashx
<%@ 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";       string filename = file.Name;       string userAgent = HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower();       //处理firefox保存时文件名乱码的问题       if (userAgent.IndexOf("firefox") == -1)           filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);         HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);       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();    }}

转载地址:http://itzrf.baihongyu.com/

你可能感兴趣的文章
剑指offer 58. 链表中环的入口结点
查看>>
剑指offer 59. 把字符串转换成整数
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-27》17.电话号码的字母组合
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>