.net 代码下载word文档


using System;
using System.Web;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;


/// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fullname">文件的绝对路径</param>
        public void DownFile(string fullname)
        {

            if (File.Exists(fullname))
            {
                FileInfo file = new FileInfo(fullname);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Lenght", file.Length.ToString());

                HttpContext.Current.Response.ContentType = "application/octet-stream";

                //以字符流的形式下载
                FileStream fs = new FileStream(fullname, FileMode.Open);
                byte[] bytes = new byte[(int)file.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                //下载
                HttpContext.Current.Response.BinaryWrite(bytes);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }

        }

posted @ 2013-12-28 17:16  牡丹  阅读(221)  评论(0)    收藏  举报