asp.net中下载文件的代码:

asp.net中下载文件的代码:
例一:
1.C#.net 
 /// <summary>
 /// 文件下载
 /// </summary>
 /// <param name="FullFileName"></param>
 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();
 }
 设置保存文件格式(example: *.xls):
 Response.ContentType = "application/ms-excel";
2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
        Try
           If File.Exists(page.MapPath(strFileName)) Then
               Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
               '清除缓冲区流中的所有内容输出.
               page.Response.Clear()
               '向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
               page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))
'繁体格式
                'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))
                '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
                page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
                '表明输出的HTTP为流[stream],因此客户端只能下载.
                page.Response.ContentType = "application/octet-stream"
                '发送文件流到客户端.
                page.Response.WriteFile(TargetFile.FullName)
                '停止执行当前页
                page.Response.End()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
例二:
 System.IO.FileStream r = new System.IO.FileStream(Server.MapPath("."+"/overlibw.js"), System.IO.FileMode.Open);
 //设置基本信息
 Response.Buffer = false;
 Response.AddHeader("Connection", "Keep-Alive");
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName));
 Response.AddHeader("Content-Length", r.Length.ToString());
 while (true)
 {
  //开辟缓冲区空间
  byte[] buffer = new byte[1024];
  //读取文件的数据
  int leng = r.Read(buffer, 0, 1024);
  if (leng == 0)//到文件尾,结束
  break;
  if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
  Response.BinaryWrite(buffer);
  else
  {
  //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
  byte[] b = new byte[leng];
  for (int i = 0; i < leng; i++)
  b[i] = buffer[i];
  Response.BinaryWrite(b);
  }
  }
  r.Close();//关闭下载文件
  Response.End();//结束文件下载
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值