Asp.net 下载功能的解决方案
程序员文章站
2024-03-09 15:24:47
1. 首先新建一个用于进行下载处理的page页,如download.aspx,里面什么东西也没有。 2. 添加一个downloadhandler类,它继承于ihttphan...
1. 首先新建一个用于进行下载处理的page页,如download.aspx,里面什么东西也没有。
2. 添加一个downloadhandler类,它继承于ihttphandler接口,可以用来自定义http 处理程序同步处理http的请求。
public class downloadhandler : ihttphandler
{
public void processrequest(httpcontext context)
{
httpresponse response = context.response;
httprequest request = context.request;
system.io.stream istream = null;
byte[] buffer = new byte[10240];
int length;
long datatoread;
try
{
string filename = filehelper.decrypt(request["fn"]); //通过解密得到文件名
string filepath = httpcontext.current.server.mappath("~/") + "files/" + filename; //待下载的文件路径
istream = new system.io.filestream(filepath, system.io.filemode.open,
system.io.fileaccess.read, system.io.fileshare.read);
response.clear();
datatoread = istream.length;
long p = 0;
if (request.headers["range"] != null)
{
response.statuscode = 206;
p = long.parse(request.headers["range"].replace("bytes=", "").replace("-", ""));
}
if (p != 0)
{
response.addheader("content-range", "bytes " + p.tostring() + "-" + ((long)(datatoread - 1)).tostring() + "/" + datatoread.tostring());
}
response.addheader("content-length", ((long)(datatoread - p)).tostring());
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachment; filename=" + system.web.httputility.urlencode(system.text.encoding.getencoding(65001).getbytes(path.getfilename(filename))));
istream.position = p;
datatoread = datatoread - p;
while (datatoread > 0)
{
if (response.isclientconnected)
{
length = istream.read(buffer, 0, 10240);
response.outputstream.write(buffer, 0, length);
response.flush();
buffer = new byte[10240];
datatoread = datatoread - length;
}
else
{
datatoread = -1;
}
}
}
catch (exception ex)
{
response.write("error : " + ex.message);
}
finally
{
if (istream != null)
{
istream.close();
}
response.end();
}
}
public bool isreusable
{
get { return true; }
}
}
3. 这里涉及到一个文件名加解密的问题,是为了防止文件具体名称暴露在状态栏中,所以添加一个filehelper类,代码如下:
public class filehelper
{
public static string encrypt(string filename)
{
byte[] buffer = httpcontext.current.request.contentencoding.getbytes(filename);
return httputility.urlencode(convert.tobase64string(buffer));
}
public static string decrypt(string encryptfilename)
{
byte[] buffer = convert.frombase64string(encryptfilename);
return httpcontext.current.request.contentencoding.getstring(buffer);
}
}
利用base64码对文件名进行加解密处理。
4. 在web.config上,添加httphandlers结点,如下:
<system.web>
<httphandlers>
<add verb="*" path="download.aspx" type="downloadhandler" />
</httphandlers>
</system.web>
5. 现在新建一个aspx页面,对文件进行下载:
default.aspx代码如下:
default.aspx
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>文件下载</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:hyperlink id="link" runat="server" text="文件下载"></asp:hyperlink>
</div>
</form>
</body>
</html>
default.aspx.cs代码如下:
default.aspx.cs
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string url = filehelper.encrypt("designpattern.chm");
link.navigateurl = "~/download.aspx?fn=" + url;
}
}
2. 添加一个downloadhandler类,它继承于ihttphandler接口,可以用来自定义http 处理程序同步处理http的请求。
复制代码 代码如下:
public class downloadhandler : ihttphandler
{
public void processrequest(httpcontext context)
{
httpresponse response = context.response;
httprequest request = context.request;
system.io.stream istream = null;
byte[] buffer = new byte[10240];
int length;
long datatoread;
try
{
string filename = filehelper.decrypt(request["fn"]); //通过解密得到文件名
string filepath = httpcontext.current.server.mappath("~/") + "files/" + filename; //待下载的文件路径
istream = new system.io.filestream(filepath, system.io.filemode.open,
system.io.fileaccess.read, system.io.fileshare.read);
response.clear();
datatoread = istream.length;
long p = 0;
if (request.headers["range"] != null)
{
response.statuscode = 206;
p = long.parse(request.headers["range"].replace("bytes=", "").replace("-", ""));
}
if (p != 0)
{
response.addheader("content-range", "bytes " + p.tostring() + "-" + ((long)(datatoread - 1)).tostring() + "/" + datatoread.tostring());
}
response.addheader("content-length", ((long)(datatoread - p)).tostring());
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachment; filename=" + system.web.httputility.urlencode(system.text.encoding.getencoding(65001).getbytes(path.getfilename(filename))));
istream.position = p;
datatoread = datatoread - p;
while (datatoread > 0)
{
if (response.isclientconnected)
{
length = istream.read(buffer, 0, 10240);
response.outputstream.write(buffer, 0, length);
response.flush();
buffer = new byte[10240];
datatoread = datatoread - length;
}
else
{
datatoread = -1;
}
}
}
catch (exception ex)
{
response.write("error : " + ex.message);
}
finally
{
if (istream != null)
{
istream.close();
}
response.end();
}
}
public bool isreusable
{
get { return true; }
}
}
3. 这里涉及到一个文件名加解密的问题,是为了防止文件具体名称暴露在状态栏中,所以添加一个filehelper类,代码如下:
复制代码 代码如下:
public class filehelper
{
public static string encrypt(string filename)
{
byte[] buffer = httpcontext.current.request.contentencoding.getbytes(filename);
return httputility.urlencode(convert.tobase64string(buffer));
}
public static string decrypt(string encryptfilename)
{
byte[] buffer = convert.frombase64string(encryptfilename);
return httpcontext.current.request.contentencoding.getstring(buffer);
}
}
利用base64码对文件名进行加解密处理。
4. 在web.config上,添加httphandlers结点,如下:
复制代码 代码如下:
<system.web>
<httphandlers>
<add verb="*" path="download.aspx" type="downloadhandler" />
</httphandlers>
</system.web>
5. 现在新建一个aspx页面,对文件进行下载:
default.aspx代码如下:
default.aspx
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>文件下载</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:hyperlink id="link" runat="server" text="文件下载"></asp:hyperlink>
</div>
</form>
</body>
</html>
default.aspx.cs代码如下:
default.aspx.cs
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string url = filehelper.encrypt("designpattern.chm");
link.navigateurl = "~/download.aspx?fn=" + url;
}
}