码迷,mamicode.com
首页 > 其他好文 > 详细

《工作日记》2019-01-29 文件下载

时间:2019-01-27 18:57:35      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:color   res   convert   文件压缩   pos   ica   adf   public   bsp   

Controller层

  public void DownLoadFiles(string ids)
        {
            try
            {
                string str = Convert.ToString(ids);
                if (ids != null && !string.IsNullOrWhiteSpace(str))
                {
                    List<string> strPath = new List<string>();
                    string[] idlist = str.Substring(0, str.Length - 1).Split(,);
                    foreach (string id in idlist)
                    {
                        var list = StudengService.LoadEntites(o => o.Stu_ID == Convert.ToInt32(id));
                        if (list != null && list.Count() > 0)
                        {
                            foreach (var model in list)
                            {
                                var detail = detailService.LoadEntites(o => o.ID == Convert.ToInt32(id)).First();
                                if (detail != null)
                                {
                                    string strAddress = model.Data_Address;
                                    strPath.Add(Server.MapPath(@"\Content\Files\") + strAddress);
                                }
                            }
                        }
                    }
                    ZipFileDownload(strPath.ToArray(), "资料文件.zip");
                }
            }
            catch (Exception ex)
            {
                
            }

        }
    
        /// <summary> 
        ///  批量进行多个文件压缩到一个文件 
        /// </summary> 
        /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者 
        /// <param name="zipFileName">生成的zip文件名称</param> 
        private void ZipFileDownload(string[] files, string zipFileName)
        {
            MemoryStream ms = new MemoryStream();
            byte[] buffer = null;

            using (ZipFile file = ZipFile.Create(ms))
            {
                file.BeginUpdate();
                file.NameTransform = new MyNameTransfom();
                foreach (var item in files)
                {
                    if (System.IO.File.Exists(item))
                    {
                        file.Add(item);
                    }
                }
                file.CommitUpdate();
                buffer = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, buffer.Length);   //读取文件内容(1次读ms.Length/1024M) 
                ms.Flush();
                ms.Close();
            }
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("content-disposition", "attachment;filename=" + zipFileName);
            Response.BinaryWrite(buffer);
            Response.Flush();
            Response.End();
        } 
 

View层

 <span class="btn"><a href="#" class="btn-download">下载</a></span>

 var jsonData = "";
            $(".checkchild:checked").each(function () {
                jsonData += $(this).val() + ",";
            });
            if (jsonData.length > 0) {
                window.location.href = "/Data/DownLoadFiles?ids=" + jsonData;
            }else{
                $.alert({
                    title: "Message",
                    content: "请勾选要下载的数据",
                });
            }

 

《工作日记》2019-01-29 文件下载

标签:color   res   convert   文件压缩   pos   ica   adf   public   bsp   

原文地址:https://www.cnblogs.com/zlkkkkkk/p/10326647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!