//TransmitFile实现下载
public void xiazai(string filePath, string yname)
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400MB的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
代码如下:
*/
try
{
//Response.Write(this.FileUpload1.PostedFile.ContentLength);
//Response.Write(this.FileUpload1.PostedFile.ContentType);
//Response.Write(System.IO.Path.GetExtension(name)); //扩展名
//Response.Write(System.IO.Path.GetFileName(fileName));//文件名
//return;
if (filePath == "" || !File.Exists(Server.MapPath(filePath)))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('暂无该文件!')</script>");
return;
}
string path = Server.MapPath(filePath);
Response.ContentType = "application/x-*-compressed";
//Response.ContentType = "application/x-zip-compressed";//保存类型
//解决乱码问题(在web.config配置文件中只配置responseEncoding='UTF-8'无效)
yname = Server.UrlEncode(yname);
// yname = HttpUtility.UrlEncode(yname);
//yname = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(yname));
Response.AddHeader("Content-Disposition", "attachment;filename=" + yname);
Response.TransmitFile(path);
// 两种解决乱码方法
// 1.如果web.config里utf-8 (gb2312不行)
// 直接Server.UrlEncode(filename)就可以
// 2.否则就要用第二种方法
// filename=HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(filename));
// 好象编码必须是 UTF8,不管您的
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//这个语句里面写的是什么编码.
}
catch
{
//打开时异常了
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('暂无该文件')</script>");
return;
}
}
//下载方法二
public void xiazai()
{
string FullFileName = "";
//ASP.NET下载文件(弹出打开保存文件对话框)
//fileURL为带路径的文件全名
/*
string fileURL = url;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileURL);
Response.Clear();
Response.AddHeader("content-disposition","attachment;filename="+Server.UrlEncode(fileInfo.Name.ToString()));
Response.AddHeader("content-length",fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.WriteFile(fileURL);
*/
//无错版:
try
{
string FileName = "file/fenye.rar";
//FileName = "分页.rar";
FullFileName = Server.MapPath(FileName);
//FileName--要下载的文件名
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
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.ASCII));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
//文件不存在
}
}
catch
{
//打开时异常了
}
}
//下载方法三:Response.BinaryWrite下载
public static bool ResponseFile1(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath)
{
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 = 500; //每秒5次 即5*10K bytes每秒,,改20K/秒 500毫秒
//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());
_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())));
//将一个2进制字符串写入到HTTP输出流,ReadBytes,从当前
Thread.Sleep(sleep);//2000是2秒,把CPU让给别的进程
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}