日期:2014-05-16 浏览次数:20397 次
FileStream fs = new FileStream(filePath , FileMode . Open);
byte[] bytes = new byte [ ( int ) fs . Length ];
fs . Read(bytes , 0 , bytes . Length);
fs . Close();
Response . ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response . AddHeader("Content-Disposition" , "attachment; filename=" + HttpUtility . UrlEncode(FileName , System . Text . Encoding . UTF8));
Response . BinaryWrite(bytes);
//Response . Close();
Response . Flush();
Response . End();
/// <summary>
/// Response.AddHeader实现下载
/// </summary>
/// <param name="filePath">完整的文件路径</param>
/// <param name="fileName">文件名</param>
private void DownFile(string filePath, string fileName)
{
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileName))););
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();