我要在页面上有个按钮,一点击,会弹出下载对话框,文件在服务器上,要下载到客户端。最好有代码。谢谢。

解决方案 »

  1.   

     protected void Button1_Click(object sender, EventArgs e)
        {
            Response.AppendHeader("Content-Disposition", "attachment;filename=File.doc");
            //输出内容的编码方式
            Response.ContentEncoding = Encoding.UTF7;        FileStream fs = new FileStream("d:/api.doc",FileMode.Open);        BinaryReader br = new BinaryReader(fs);        byte[] docbyte=br.ReadBytes(1000);        while (docbyte.Length > 0)
            {
                Response.BinaryWrite(docbyte);
                
                docbyte = br.ReadBytes(1000);
            }
            Response.Flush();
            Response.Close();
            Response.End();        
        }
      

  2.   

     /// <summary>
        /// 從WEB頁面文件下載
        /// </summary>
        /// <param name="strFile">要下載的文件的絕對路徑</param>
        public static void DownloadFile(string strFile)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Path.GetFileName(strFile).Trim()) + "\"");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.WriteFile(strFile);
            HttpContext.Current.Response.End();
        }
      

  3.   


    <input type="button" value="Try it." onclick="btn_click();">
    <script>
    function btn_click()
    {
         window.open("../FileDownload.html?FileName=FuckJP.mp3");
    }
    </script>
      

  4.   

    有空看看,http://www.cnblogs.com/jiangshaofen/archive/2007/04/16/715227.html