.NET实现电子文件的单个下载及批量下载

今天遇到电子文件单个下载及批量下载的问题,以下是总结出来的部分代码。

单个电子文件下载:

      /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="page">当前页对象</param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileContext">文件字节流</param>
        public static void DownLoadFile(System.Web.UI.Page page, string fileName, byte[] fileContext)
        {
            page.Response.Buffer = true;
            page.Response.Clear();
            page.Response.ContentType = "application/octet-stream";
            page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
            page.Response.BinaryWrite(fileContext);
            page.Response.Flush();
            page.Response.End();
        }

使用using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

打包下载:

#region 批量下载选中的电子文件
        /// <summary>
        /// 批量下载
        /// </summary>
        /// <param name="selectedID">所选ID集合</param>
        private bool BatchDownLoad(string fid)
        {
            bool result = true;
            GDAS.Model.Sys_User curUser = GDAS.BLL.Sys_User.GetCurUser();
            string NewFilename = "[" + curUser.No + "] " + curUser.Name + ".zip";
            string strTempPath = getFuPath();

            if (!System.IO.Directory.Exists(strTempPath))
            {
                System.IO.Directory.CreateDirectory(strTempPath);
            }
            string strFullPath = Path.Combine(strTempPath, NewFilename);
            //先删除文件 再生成
            if (System.IO.File.Exists(strFullPath))
            {
                System.IO.File.Delete(strFullPath);
            }

            GDAS.BLL.Doc_Attachment bAttachment = new GDAS.BLL.Doc_Attachment();
            result = bAttachment.ZipFileList(fid, strFullPath);
            if (result)
            {
                string strFileDownloadName = "附件打包下载.zip";
                Response.ContentType = "application/x-zip-compressed";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileDownloadName, System.Text.Encoding.UTF8));
                Response.TransmitFile(strFullPath);
            }
            else
            {
                Kit.Ajax_Alert(this, "附件打包出错,请与管理员联系!");
            }

            return result;
        }

        //获取下载打包临时文件路径
        private string getFuPath()
        {
            return Server.MapPath(Config.DownloadPath);
        }
        #endregion

 

 /// <summary>
        /// 批量下载附件
        /// </summary>
        /// <param name="list">批量下载的附件ID集合</param>
        /// <param name="ZipedFile">压缩后的文件路径全称</param>
        /// <param name="downloaderNo">下载用户的员工号</param>
        /// <param name="downloaderName">下载用户的姓名</param>
        public bool ZipFileList(string strID, string strFullpath)
        {
            try
            {
                System.IO.FileStream ZipFile = System.IO.File.Create(strFullpath);
                ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);

                string sql = "select FileName,FileContext from db_owner.Doc_Attachment where ID in (" + strID + ")";
                DataTable dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        byte[] FileContext = dr["FileContext"] as byte[];
                        string strFileName = dr["FileName"].ToString();
                        ZipEntry ZipEntry = new ZipEntry(strFileName);
                        ZipStream.PutNextEntry(ZipEntry);
                        ZipStream.SetLevel(6);
                        ZipStream.Write(FileContext, 0, FileContext.Length);
                    }
                }
                ZipStream.Finish();
                ZipStream.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

 

转载于:https://www.cnblogs.com/StevenDu/archive/2013/04/17/DownLoad.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值