Web DownLoad 方法

第一种

    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使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 佛,不是有难时所求生穷时求财,佛该是一种信仰, 让你难时保持平静,穷时找到追求。佛不等于你迷信,很多人没有明白这个道理。
    流浪者之歌01阅读 178评论 0 1
  • 今天上午,郑州航空港区领航学校的孙军红老师为我们讲了教育叙事与教师的专业成长,她最后所总结的一句话给我印象...
    汤汤人阅读 1,188评论 0 3
  • 最近再做一个项目时,发现UILabel中text的系统默认行间距不能满足要求,于是在网上找到了调整行间距的代码。跟...
    Japho阅读 3,446评论 0 1
  • 今天下载了个weex demo,稍微研究了下运行和生成的apk. 首先用android机子,手头上是一台5.1的魅...
    _稀阅读 2,630评论 0 1
  • 10.1外部性和市场无效率。外部性是指一个人的行为对旁观者福利的无补偿影响。如果影响是不利的,就是负外部性,如果影...
    sunflower_d66c阅读 97评论 0 0