文件批量下载到本地

1. 添加dll库DotNetZip

public ActionResult DownloadFile(string documentno)
        {
            if (!string.IsNullOrEmpty(documentno))
            {
                var list =
                    _borrowerContractImageRepository.GetAlList(documentno);
                if (list.Count > 0)
                {
                    //获取服务器中的文件路径
                    string filePath =Configs.GetFileSite();

                    //压缩后的文件存放路径
                    string destFile = @"E:\文件\" + documentno + @"\";
                    if (!Directory.Exists(destFile))//判断文件夹是否存在
                    {
                        Directory.CreateDirectory(destFile);//不存在则创建文件夹
                    }
                    using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
                    {
                        //单个文件下载,不需要打包
                        foreach (var fileName in list)
                        {
                            WebClient wc=new WebClient();  
                            wc.DownloadFile(filePath + fileName,destFile+Path.GetFileName(fileName));
                            zip.AddFile(destFile + Path.GetFileName(fileName), "Images");

                        }
                        zip.Save(Server.MapPath("~/ZIP/"+documentno+".zip"));
                    }
                    
                    return File(Server.MapPath("~/ZIP/"+documentno + ".zip"),
                                               "application/zip", documentno+".zip");
                }
                else
                {
                    return Content("没有文件可以下载");
                }
            }
            else
            {
                return Content("表单编号异常");
            }
        }

posted @ 2017-02-23 11:31  sueris  阅读(329)  评论(0编辑  收藏  举报