asp.net 打开新页面提示并下载安装文件

(一)第一种方法:

//aspx 页面里要写得代码

<script type="text/javascript">
    function download()
    {
       var p; 
        try 
        { 
          p = new ActiveXObject('AcroExch.Document');
          }
          catch (e)
          {
          
                if(confirm('系统检测到您未安装Adobe Reader 8.0或ie安全级别过高,这会使您无法正常观看和打印报表,是否下载安装?'))
                {
                    window.open("http://%22+window.location.host+%22/soft/AdbeRdr80_zh_CN.exe");
                }
          }
      }
    </script>

//aspx.cs 文件下要写得代码string ip = "";
IPHostEntry strHostName = Dns.Resolve(Dns.GetHostName());
ip = strHostName.AddressList[0].ToString();
if (ip == "127.0.0.1")
ip = Dns.GetHostName();

 

(二)第二种方法:

   //打开要下载的文件
    System.IO.FileStream r = new System.IO.FileStream(FilePath, 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(FilePath));
    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();//结束文件下载

posted on 2008-09-19 10:31  一江秋水  阅读(655)  评论(0编辑  收藏  举报