Web DownLoad 方法

2018-04-28  本文已影响16人  SMILE_NO_09

第一种

    public static class DownloadHelper
    {
        public static void Download(string fileFullPath)
        {
            FileInfo file = new FileInfo(fileFullPath);

             HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", file.Name));      

            HttpContext.Current.Response.WriteFile(fileFullPath);

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }

第二种

class DownLoadHelper
    {
        private HttpContextBase _httpContext = null;
        public DownLoadHelper(HttpContextBase httpContext)
        {
            _httpContext = httpContext;
        }

        /// <summary>
        /// 使用OutputStream.Write分块下载文件,参数为文件绝对路径
        /// </summary>
        /// <param name="FileName"></param>
        public  void DownLoad(string fileFullPath)
        {
            //string filePath = MapPathFile(FileName);
            //指定块大小
            long chunkSize = 204800;
            //建立一个200K的缓冲区
            byte[] buffer = new byte[chunkSize];
            //已读的字节数
            long dataToRead = 0;
            FileStream stream = null;
            try
            {
                //打开文件
                stream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                dataToRead = stream.Length;
                //添加Http头
                _httpContext.Response.ContentType = "application/octet-stream";
                _httpContext.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(fileFullPath)));
                _httpContext.Response.AddHeader("Content-Length", dataToRead.ToString());
                while (dataToRead > 0)
                {
                    if (_httpContext.Response.IsClientConnected)
                    {
                        int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
                        _httpContext.Response.OutputStream.Write(buffer, 0, length);
                        _httpContext.Response.Flush();
                        //HttpContext.Current.Response.Clear();
                        buffer = new Byte[chunkSize];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
                        //防止client失去连接
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //HttpContext.Current.Response.Write("Error:" + ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                _httpContext.Response.Close();
            }
        }
    }

注意调用Action的时候要用a标签link使用

上一篇 下一篇

猜你喜欢

热点阅读