62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string baseFolder = System.Web.HttpContext.Current.Server.MapPath(".");
baseFolder += "\\wdt_m21\\Files_in\\lhpy\\";
string fn = "LHPY_0659H4FCRW2011_05_06_19_26_589UCIV040C5_271719373042.pdf";
FileDownload(baseFolder, fn, "", "");
}
public static void FileDownload(string FilePath, string FileName, string FileType, string FileTitle)
{
string strFullFileName = FilePath + FileName;
long chunkSize = 102400; //建立一个100K的缓冲区
byte[] buffer = new byte[chunkSize];
long dataToRead = 0;//已读的字节数
FileStream stream = null;
stream = new FileStream(strFullFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
if (FileType == "")
{
FileType = FileName.Substring(FileName.LastIndexOf(".") + 1);
}
switch (FileType.ToString().ToUpper())
{
case "XLS":
HttpContext.Current.Response.ContentType = "application/ms-excel";
break;
case "DOC":
HttpContext.Current.Response.ContentType = "application/ms-word";
break;
case "TXT":
HttpContext.Current.Response.ContentType = "application/text";
break;
case "PDF":
HttpContext.Current.Response.ContentType = "application/pdf";
break;
case "GIF":
HttpContext.Current.Response.ContentType = "images/gif";
break;
case "JPG":
HttpContext.Current.Response.ContentType = "images/jpg";
break;
}
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
//HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
else
{
dataToRead = -1; //防止client失去连接
}
}
//HttpContext.Current.Response.End();
}
}
string newFileName = 新文件名;
string downUrl = Server.MapPath(虚拟路径);
FileInfo downFile = new FileInfo(downUrl);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(newFileName));
Response.AddHeader("Content-Length", downFile.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(downFile.FullName);
Response.Flush();
Response.End();
//这里,唯一让服务器持续输出数据的理由只有两个:当前下载请求仍然没有中断,且数据没有输出完毕
while (HttpContext.Current.Response.IsClientConnected && dataToRead > 0)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
//HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
}
finally
{
//HttpContext.Current.Response.Close();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
public static void FileDownload(string FilePath, string FileName, string FileType, string FileTitle)
{
try
{
string strFullFileName = FilePath + FileName;
long chunkSize = 102400; //建立一个100K的缓冲区
byte[] buffer = new byte[chunkSize];
long dataToRead = 0;//已读的字节数
using (FileStream stream = new FileStream(strFullFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
dataToRead = stream.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
if (FileType == "")
{
FileType = FileName.Substring(FileName.LastIndexOf(".") + 1);
}
switch (FileType.ToString().ToUpper())
{
case "XLS":
HttpContext.Current.Response.ContentType = "application/ms-excel";
break;
case "DOC":
HttpContext.Current.Response.ContentType = "application/ms-word";
break;
case "TXT":
HttpContext.Current.Response.ContentType = "application/text";
break;
case "PDF":
HttpContext.Current.Response.ContentType = "application/pdf";
break;
case "GIF":
HttpContext.Current.Response.ContentType = "images/gif";
break;
case "JPG":
HttpContext.Current.Response.ContentType = "images/jpg";
break;
}
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
else
{
dataToRead = -1; //防止client失去连接
}
}
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Error.txt");
HttpContext.Current.Response.Write("下载失败!");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
finally
{
HttpContext.Current.Response.Close();
}
}
public static void FileDownload(string FilePath,string FileName,string FileType, string FileTitle)
{
string strFullFileName = FilePath + FileName ;
long chunkSize = 102400; //建立一个100K的缓冲区
byte[] buffer = new byte[chunkSize];
long dataToRead = 0;//已读的字节数
FileStream stream = null;
try
{
using (stream = new FileStream(strFullFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
dataToRead = stream.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
if (FileType == "")
{
FileType = FileName.Substring(FileName.LastIndexOf(".") + 1);
}
switch (FileType.ToString().ToUpper())
{
case "XLS":
HttpContext.Current.Response.ContentType = "application/ms-excel";
break;
case "DOC":
HttpContext.Current.Response.ContentType = "application/ms-word";
break;
case "TXT":
HttpContext.Current.Response.ContentType = "application/text";
break;
case "PDF":
HttpContext.Current.Response.ContentType = "application/pdf";
break;
case "GIF":
HttpContext.Current.Response.ContentType = "images/gif";
break;
case "JPG":
HttpContext.Current.Response.ContentType = "images/jpg";
break;
}
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
else
{
dataToRead = -1; //防止client失去连接
}
}
}
}
catch(Exception ex)
{
SysOverlook mylog = new SysOverlook();
mylog.RecordSysError("FileDownload", ex.Message);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Error.txt");
HttpContext.Current.Response.Write("下载失败!");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
finally
{
if (stream != null)
{
stream.Close();
}
HttpContext.Current.Response.Close();
}
}