项目--下载文件

/// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="FullFileName"></param>
        public static void FileDownload(string FullFileName)
        {
            FileInfo DownloadFile = new FileInfo(FullFileName); //设置要下载的文件
          
             HttpContext.Current.Response.Clear(); //清除缓冲区流中的所有内容输出
             HttpContext.Current.Response.ClearHeaders(); //清除缓冲区流中的所有头
             HttpContext.Current.Response.Buffer = false; //设置缓冲输出为false
            //设置输出流的 HTTP MIME 类型为application/octet-stream
             HttpContext.Current.Response.ContentType = "application/octet-stream";
            //将 HTTP 头添加到输出流
            string s = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(DownloadFile.Name));
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + s);// HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));

            HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

            //将指定的文件直接写入 HTTP 内容输出流。

            HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
            HttpContext.Current.Response.Flush(); //向客户端发送当前所有缓冲的输出
            HttpContext.Current.Response.End(); //将当前所有缓冲的输出发送到客户端

        }

调用代码:

string path = "/UploadFile/Template/ImportApplyInfo.xlsx";//目录为当前web下目录
FileDownload(this.Server.MapPath(path));

 

posted @ 2014-07-29 11:05  步子521  阅读(214)  评论(0编辑  收藏  举报