一个页面上有个LinkButton(A.doc)
A.doc是从数据库中读取的数据作为一个LinkButtonA.doc在项目工程根目录下面的ABC文件夹下如何点击这个LinkButton实现打开或下载A.doc这个文档?

解决方案 »

  1.   

    LinkButton的Click事件中写入string fileName = "xx.doc";//客户端保存的文件名
                string filePath = Server.MapPath("/ABC/a.doc");//路径            //以字符流的形式下载文件
                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.Flush();
                Response.End();
      

  2.   

    直接链接文件:
    string url_str = "~/ABC/A.doc";
    Response.Redirect(url_str);
      

  3.   

    直接超链接<a href="ABC/a.doc"></a>