asp教程.net文件下载类
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>asp.net教程文件下载类</title>
</head>
<body>
<%
'件下载类是什么意思?
实现下载功能?
DownloadData 方法通过 address 参数指定的 URI 下载资源。此方法在下载资源时阻止。若要下载资源并在等待服务器响应的同时继续执行,请使用 DownloadDataAsync 方法之一。
如果 BaseAddress 属性不是空字符串 (""),且 address 不包含绝对 URI,则 address 必须是相对 URI,此 URI 与 BaseAddress 组合在一起构成所请求数据的绝对 URI。如果 QueryString 属性不是空字符串,则将它追加到 address。
此方法使用 RETR 命令下载 FTP 资源。对于 HTTP 资源,使用 GET 方法。
下面的代码示例从服务器请求数据并显示所返回的数据。它假设 remoteUri 包含所请求的数据的有效 URI。
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
调用方法
string path= Server.MapPath("fujian/"+文件名称);//path为服务器存放文件的路径
FileDownload(path);
'文件下载类方法二
public class FileHelper
{
/// <summary>
/// 利用Base64码对文件名进行加密处理。
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static string Encrypt(string filename)
{
byte[] buffer = HttpContext.Current.Request.ContentEncoding.GetBytes(filename);
return HttpUtility.UrlEncode(Convert.ToBase64String(buffer));
}
/// <summary>
/// 利用Base64码对文件名进行解密处理。
/// </summary>
/// <param name="encryptfilename">文件名</param>
/// <returns></returns>
public static string Decrypt(string encryptfilename)
{
byte[] buffer = Convert.FromBase64String(encryptfilename);
return HttpContext.Current.Request.ContentEncoding.GetString(buffer);
}
}
'方法三
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
double pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
'调用 方法在Page_Load加入
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "目的文件名称", @"源文件路径", 1024000);
if (!success)
Response.Write("下载文件出错!");
Page.Response.End();
%>
</body>
</html>