在WEB项目中,实现word文档下载功能

首先,在项目中创建一个文件夹,用于存放文件,这里把文件命名为FileInfo

在需要实现功能的页面中,

1.前台的文字部分使用,LinkButton控件,然后进行数据绑定

2.后台代码部分,

      1>引用命名控件,using System.IO;

      2>创建方法

            public void DownLoad(string name){    
           
                   string filePath = Server.MapPath( @"\FileInfo\" + name );//这里注意了,你得指明要下载文件的路径.    
           
                   if ( System.IO.File.Exists( filePath ) )    
                  {         
               
                         FileInfo file = new FileInfo( filePath );         
                         Response.ContentEncoding = System.Text.Encoding.GetEncoding( "UTF-8" ); //解决中文乱码     
                         Response.AddHeader( "Content-Disposition", "attachment; filename=" + Server.UrlEncode( file.Name ) ); //解决中文文件名乱码         
                         Response.AddHeader( "Content-length", file.Length.ToString() );         
                         Response.ContentType = "appliction/octet-stream";         
                         Response.WriteFile( file.FullName );         
                         Response.End();         
                   }
            }

 

  3>实现的时候,使用LinkButton的单击事件,然后调用方法

    protected void LinkButton1_Click(object sender, EventArgs e)
           {
                  DownLoad(LinkButton1.Text);
           }

 

posted @ 2013-05-12 15:58  殇琉璃  阅读(595)  评论(0编辑  收藏  举报