C#多文件通过文件流形式与文件格式打包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012538990/article/details/83447392

文件打包:

  var entity = _signUpRepository.Get(Convert.ToInt64(id));//不包含草稿
            string matchName = entity.SubjectName + "-第二届“豫创天下”创业创新大赛报名详情";
            #region 导入同一文件夹=========
            MemoryStream ms = new MemoryStream(); //最终文件
            using (ZipFile zip = ZipFile.Create(ms))
            {
                var model = entity;
                //基本信息以Excel形式导出
                IWorkbook workbook = GetExcelInformation(model);
                MemoryStream excelms = new MemoryStream();
                workbook.Write(excelms);
                zip.BeginUpdate();
                StreamDataSource sd = new StreamDataSource(excelms);
                zip.Add(sd, "报名详细信息.xlsx");
                string filetype = "";//文件格式
                                     //营业执照扫描件 
                if (System.IO.File.Exists(Server.MapPath(model.BusinessLicenseImg)))
                {
                    try
                    {
                        filetype = model.BusinessLicenseImg.Split('.')[1];
                        zip.Add(Server.MapPath(model.BusinessLicenseImg), "营业执照扫描件." + filetype);
                    }
                    catch { }
                }
                //组织机构代码证
                if (System.IO.File.Exists(Server.MapPath(model.OrganizationImg)))
                {
                    try
                    {
                        filetype = model.OrganizationImg.Split('.')[1];
                        zip.Add(Server.MapPath(model.OrganizationImg), "组织机构代码证." + filetype);
                    }
                    catch { }
                }
                //税务登记证
                if (System.IO.File.Exists(Server.MapPath(model.TaxImg)))
                {
                    try
                    {
                        filetype = model.TaxImg.Split('.')[1];
                        zip.Add(Server.MapPath(model.TaxImg), "税务登记证." + filetype);
                    }
                    catch { }
                }
                //法人身份证照正面
                if (System.IO.File.Exists(Server.MapPath(model.IdentityUp)))
                {
                    try
                    {
                        filetype = model.IdentityUp.Split('.')[1];
                        zip.Add(Server.MapPath(model.IdentityUp), "法人身份证照正面." + filetype);
                    }
                    catch { }
                }
                //法人身份证照反面
                if (System.IO.File.Exists(Server.MapPath(model.IdentityBottom)))
                {
                    try
                    {
                        filetype = model.IdentityBottom.Split('.')[1];
                        zip.Add(Server.MapPath(model.IdentityBottom), "法人身份证照反面." + filetype);
                    }
                    catch { }
                }
                //创业计划书
                if (System.IO.File.Exists(Server.MapPath(model.PlanUrl)))
                {
                    try
                    {
                        filetype = model.PlanUrl.Split('.')[1];
                        zip.Add(Server.MapPath(model.PlanUrl), "创业计划书." + filetype);
                    }
                    catch { }
                }
                //路演PPT
                if (System.IO.File.Exists(Server.MapPath(model.PPTUrl)))
                {
                    try
                    {
                        filetype = model.PPTUrl.Split('.')[1];
                        zip.Add(Server.MapPath(model.PPTUrl), "路演PPT." + filetype);
                    }
                    catch { }
                }
                //附件
                if (System.IO.File.Exists(Server.MapPath(model.AttachmentUrl)))
                {
                    try
                    {
                        filetype = model.AttachmentUrl.Split('.')[1];
                        zip.Add(Server.MapPath(model.AttachmentUrl), "附件." + filetype);
                    }
                    catch { }
                }
                zip.CommitUpdate();
            }
            #endregion ====================
            ExportFile(matchName + ".zip", ms);

StreamDataSource文件流转换

 public class StreamDataSource : IStaticDataSource
    {
        public byte[] bytes { get; set; }
        public StreamDataSource(MemoryStream ms)
        {
            bytes = ms.GetBuffer();
        }
        public Stream GetSource()
        {
            Stream s = new MemoryStream(bytes);
            return s;
        }
    }

导出数据

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="ms"></param>
        /// <returns></returns>
        private string ExportFile(string fileName, System.IO.MemoryStream ms)
        {
            string msg = null;
            try
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
                Response.Buffer = true;
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(ms.ToArray());
                ms.Close();
                ms.Dispose();
                Response.Flush();
                Response.Close();
                msg = "导出成功";
            }
            catch (Exception ex)
            { msg = ex.Message; }
            return msg;
        }

猜你喜欢

转载自blog.csdn.net/u012538990/article/details/83447392