标签归档:文件下载

asp.net下载文件几种方式

以下是实现代码:

<pre>protected void Button1_Click(object sender, EventArgs e)
{
  /*
    微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
   下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
   代码如下:
  */
   Response.ContentType = "application/x-zip-compressed";
   Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
   string filename = Server.MapPath("DownLoad/aaa.zip");
   Response.TransmitFile(filename);
}

//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
  /*
   using System.IO;
  */

  string fileName ="aaa.zip";//客户端保存的文件名
  string filePath=Server.MapPath("DownLoad/aaa.zip");//路径

  FileInfo fileInfo = new FileInfo(filePath);
  Response.Clear();
  Response.ClearContent();
  Response.ClearHeaders();
  Response.AddHeader("Content-Disposition", "attachment;filename=" + 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();
  Response.End();
}</pre>

继续阅读