• .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;
                }

            }

  • 相关阅读:
    四种方案解决ScrollView嵌套ListView问题
    Android学习笔记之Intent
    Struts2中 radio标签的详细使用方法
    Qrcode生成二维码的参数总结 及最小尺寸的测试
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    HTTP中的重定向和请求转发的区别
    SpringMVC(五)-- springmvc的系统学习之拦截器
    SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful
    Github上的star和fork是什么
    如何在MyEclipse上耍Chrome
  • 原文地址:https://www.cnblogs.com/StevenDu/p/DownLoad.html
Copyright © 2020-2023  润新知