官方QQ群:127876820【C#编程技术-全国站--未满人】

ASP.NET实现批量下载文件及附件的方法详细讲解

应用场景

发文管理员发送文件给用户,在发文的过程中,可以上传附件;作为普通用户,需要对收到的公司发文批量下载,下载要求能够下载公司发文的文件和附件。

问题难点:

(1)对多个文件及其附件进行打包,附件要和公司文件放在一起

(2)文件夹、文件命名的确定,因为使用原文件名,可能有重复

(3)对文件夹(包含文件及子文件夹,子文件夹下又有文件)进行打包

解决思路:

(1)创建和当前用户登陆名同名的临时文件夹,这个文件夹里存储了用户选择批量下载的所有文件及附件,临时文件夹名称以当前用户的登陆名命名,如张三的登陆名为zhangsan,则创建文件夹zhangsan

(2)在zhangsan文件夹里根据选择的公司文件数量创建子文件夹,选中了多少个公司文件就创建多少个子文件夹,子文件夹的命名采用公司文件的原名来命名,如公司文件为:2010060301公司文件.txt,发文后的文件名称为:4fe1c0ec-bd6c-4a76-9d49-f1745c7390db.txt,如果有重复,则进行处理。

(3)如果单个公司文件有附件,则在子文件夹下创建一个attchments文件夹,用于存储附件,如果没有附件则不创建
(4)临时文件夹及文件的创建完成后,使用file.copyto()的方法把文件及附件拷贝到相应的文件夹里,由于在上传的时候采用重命名,因为拷贝完成后,还要对临时文件夹里的文件进行重命名,使用的方法是file.move()/file.moveto()方法。

(5)打包文件夹,生成压缩格式的文件
(6)点击下载文件

核心代码:

private void DownLoadCompressFile()
        {
            //create file package
            List<CompanyFileDomain> lists = new List<CompanyFileDomain>();
            if (DeluxeGridFiles.SelectedKeys.Count > 0)
            {
                for (int i = 0; i < DeluxeGridFiles.SelectedKeys.Count; i++)
                {
                    CompanyFileDomain companyFile = CompanyFileAdapter.Instance.Load(DeluxeGridFiles.SelectedKeys[i]);
                    lists.Add(companyFile);
                }
            }
            BatchFiles batch = new BatchFiles(DeluxeIdentity.CurrentUser.LogOnName);
            if (lists != null)
            {
                batch.CreatePackageByCompanyFileDomain(lists);
            }
            //compress package
            string filepath =CompanyFileConfig.Instance.FileSavePath + "\\" + DeluxeIdentity.CurrentUser.LogOnName;
            string filefolder=filepath  + "\\";
            string zipfilename =filepath + ".zip";
            if (Directory.Exists(filefolder))
            {
                FastZip fastZip = new FastZip();
                //zip filename is full file name
                fastZip.CreateZip(zipfilename, filefolder, true, "");
            }
            FileInfo zipfile = new FileInfo(zipfilename);
            if (zipfile.Exists && Directory.Exists(filefolder))
            {
                DirectoryInfo di = new DirectoryInfo(filefolder);
                di.Delete(true);
                //Directory.Delete(filefolder);
            }
            //download zip file
            Response.Redirect("batchdown.aspx?FullFilePath=" + HttpUtility.UrlEncode(zipfilename) + "&FileName=" + HttpUtility.UrlEncode(DeluxeIdentity.CurrentUser.LogOnName + ".zip") + "");//传递参数到下载压缩包的页面,下载完成后把生成的压缩包删除掉
        }

这里的创建压缩文件夹采用了ICSharpCode.SharpZipLib.Zip,其下载地址是:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

这里调用了 fastZip.CreateZip(zipfilename, filefolder, true, "");来创建压缩包,注意这里的第一个参数要写存储的文件的全路径,否则不能生成压缩文件

 

batch.CreatePackageByCompanyFileDomain(lists);
这里是根据选择的文件创建临时文件包

 

        /// <summary>
        /// 根据文件列表创建下载文件夹,并在文件夹里放置所有文件及其附件
        /// </summary>
        /// <param name="companyfiles"></param>
        public void CreatePackageByCompanyFileDomain(List<CompanyFileDomain> companyfiles)
        {
            if (companyfiles.Count > 0)
            {
                foreach (CompanyFileDomain fileEntity in companyfiles)
                {
                    CreatePackageByCompanyFileDomain(fileEntity);
                }
            }
        }
        /// <summary>
        /// 根据文件ID创建单个文件夹,并在其中放置文件及其附件
        /// </summary>
        /// <param name="resourceID"></param>
        public void CreatePackageByCompanyFileDomain(CompanyFileDomain companyfile)
        {
            if (companyfile != null)
            {
                if (!Directory.Exists(downLoadPath))
                {
                    Directory.CreateDirectory(downLoadPath);
                }
                string sourcefileNameWithoutExtend = companyfile.FileName.Substring(0,companyfile.FileName.Length-companyfile.ExtendFileName.Length);
                string sourcefileFullPath = RootFilePath + companyfile.RelativePath + "\\" + companyfile.ID + companyfile.ExtendFileName;
                string desfileFolder = downLoadPath + sourcefileNameWithoutExtend;
                string desfileattchFolder = string.Empty;

                if (Directory.Exists(desfileFolder))
                {
                    desfileFolder = RenameFolder(desfileFolder);
                }
                desfileattchFolder = desfileFolder + "http://www.cnblogs.com/yungboy/admin/file://attchments//";
                Directory.CreateDirectory(desfileFolder);
                FileInfo newFile = new FileInfo(sourcefileFullPath);
                newFile.CopyTo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                FileInfo tempFile = new FileInfo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                tempFile.MoveTo(Path.Combine(desfileFolder, companyfile.FileName));
                //have attchement
                MaterialList materials = MaterialAdapter.Instance.LoadMaterialsByResourceID(companyfile.ID);
                if (materials.Count > 0)
                {
                    Directory.CreateDirectory(desfileattchFolder);
                    foreach (Material ma in materials)
                    {
                        string sourceAttchFullPath = RootAttchPath.Remove(RootAttchPath.Length - 1, 1);
                      
                        sourceAttchFullPath += ma.RelativeFilePath;
                        FileInfo attFile = new FileInfo(sourceAttchFullPath);
                        attFile.CopyTo(desfileattchFolder + ma.ID + attFile.Extension);
                        FileInfo tempAtt = new FileInfo(desfileattchFolder + ma.ID + attFile.Extension);
                        tempAtt.MoveTo(desfileattchFolder + ma.OriginalName);//重命名的实现
                    }
                }
            }
        }


文章来自学IT网:http://www.xueit.com/asp.net/show-6916-2.aspx

核心代码:

private void DownLoadCompressFile()
        {
            //create file package
            List<CompanyFileDomain> lists = new List<CompanyFileDomain>();
            if (DeluxeGridFiles.SelectedKeys.Count > 0)
            {
                for (int i = 0; i < DeluxeGridFiles.SelectedKeys.Count; i++)
                {
                    CompanyFileDomain companyFile = CompanyFileAdapter.Instance.Load(DeluxeGridFiles.SelectedKeys[i]);
                    lists.Add(companyFile);
                }
            }
            BatchFiles batch = new BatchFiles(DeluxeIdentity.CurrentUser.LogOnName);
            if (lists != null)
            {
                batch.CreatePackageByCompanyFileDomain(lists);
            }
            //compress package
            string filepath =CompanyFileConfig.Instance.FileSavePath + "\\" + DeluxeIdentity.CurrentUser.LogOnName;
            string filefolder=filepath  + "\\";
            string zipfilename =filepath + ".zip";
            if (Directory.Exists(filefolder))
            {
                FastZip fastZip = new FastZip();
                //zip filename is full file name
                fastZip.CreateZip(zipfilename, filefolder, true, "");
            }
            FileInfo zipfile = new FileInfo(zipfilename);
            if (zipfile.Exists && Directory.Exists(filefolder))
            {
                DirectoryInfo di = new DirectoryInfo(filefolder);
                di.Delete(true);
                //Directory.Delete(filefolder);
            }
            //download zip file
            Response.Redirect("batchdown.aspx?FullFilePath=" + HttpUtility.UrlEncode(zipfilename) + "&FileName=" + HttpUtility.UrlEncode(DeluxeIdentity.CurrentUser.LogOnName + ".zip") + "");//传递参数到下载压缩包的页面,下载完成后把生成的压缩包删除掉
        }

这里的创建压缩文件夹采用了ICSharpCode.SharpZipLib.Zip,其下载地址是:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

这里调用了 fastZip.CreateZip(zipfilename, filefolder, true, "");来创建压缩包,注意这里的第一个参数要写存储的文件的全路径,否则不能生成压缩文件

 

batch.CreatePackageByCompanyFileDomain(lists);
这里是根据选择的文件创建临时文件包

 

        /// <summary>
        /// 根据文件列表创建下载文件夹,并在文件夹里放置所有文件及其附件
        /// </summary>
        /// <param name="companyfiles"></param>
        public void CreatePackageByCompanyFileDomain(List<CompanyFileDomain> companyfiles)
        {
            if (companyfiles.Count > 0)
            {
                foreach (CompanyFileDomain fileEntity in companyfiles)
                {
                    CreatePackageByCompanyFileDomain(fileEntity);
                }
            }
        }
        /// <summary>
        /// 根据文件ID创建单个文件夹,并在其中放置文件及其附件
        /// </summary>
        /// <param name="resourceID"></param>
        public void CreatePackageByCompanyFileDomain(CompanyFileDomain companyfile)
        {
            if (companyfile != null)
            {
                if (!Directory.Exists(downLoadPath))
                {
                    Directory.CreateDirectory(downLoadPath);
                }
                string sourcefileNameWithoutExtend = companyfile.FileName.Substring(0,companyfile.FileName.Length-companyfile.ExtendFileName.Length);
                string sourcefileFullPath = RootFilePath + companyfile.RelativePath + "\\" + companyfile.ID + companyfile.ExtendFileName;
                string desfileFolder = downLoadPath + sourcefileNameWithoutExtend;
                string desfileattchFolder = string.Empty;

                if (Directory.Exists(desfileFolder))
                {
                    desfileFolder = RenameFolder(desfileFolder);
                }
                desfileattchFolder = desfileFolder + "http://www.cnblogs.com/yungboy/admin/file://attchments//";
                Directory.CreateDirectory(desfileFolder);
                FileInfo newFile = new FileInfo(sourcefileFullPath);
                newFile.CopyTo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                FileInfo tempFile = new FileInfo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                tempFile.MoveTo(Path.Combine(desfileFolder, companyfile.FileName));
                //have attchement
                MaterialList materials = MaterialAdapter.Instance.LoadMaterialsByResourceID(companyfile.ID);
                if (materials.Count > 0)
                {
                    Directory.CreateDirectory(desfileattchFolder);
                    foreach (Material ma in materials)
                    {
                        string sourceAttchFullPath = RootAttchPath.Remove(RootAttchPath.Length - 1, 1);
                      
                        sourceAttchFullPath += ma.RelativeFilePath;
                        FileInfo attFile = new FileInfo(sourceAttchFullPath);
                        attFile.CopyTo(desfileattchFolder + ma.ID + attFile.Extension);
                        FileInfo tempAtt = new FileInfo(desfileattchFolder + ma.ID + attFile.Extension);
                        tempAtt.MoveTo(desfileattchFolder + ma.OriginalName);//重命名的实现
                    }
                }
            }
        }


文章来自学IT网:http://www.xueit.com/asp.net/show-6916-2.aspx

核心代码:

private void DownLoadCompressFile()
        {
            //create file package
            List<CompanyFileDomain> lists = new List<CompanyFileDomain>();
            if (DeluxeGridFiles.SelectedKeys.Count > 0)
            {
                for (int i = 0; i < DeluxeGridFiles.SelectedKeys.Count; i++)
                {
                    CompanyFileDomain companyFile = CompanyFileAdapter.Instance.Load(DeluxeGridFiles.SelectedKeys[i]);
                    lists.Add(companyFile);
                }
            }
            BatchFiles batch = new BatchFiles(DeluxeIdentity.CurrentUser.LogOnName);
            if (lists != null)
            {
                batch.CreatePackageByCompanyFileDomain(lists);
            }
            //compress package
            string filepath =CompanyFileConfig.Instance.FileSavePath + "\\" + DeluxeIdentity.CurrentUser.LogOnName;
            string filefolder=filepath  + "\\";
            string zipfilename =filepath + ".zip";
            if (Directory.Exists(filefolder))
            {
                FastZip fastZip = new FastZip();
                //zip filename is full file name
                fastZip.CreateZip(zipfilename, filefolder, true, "");
            }
            FileInfo zipfile = new FileInfo(zipfilename);
            if (zipfile.Exists && Directory.Exists(filefolder))
            {
                DirectoryInfo di = new DirectoryInfo(filefolder);
                di.Delete(true);
                //Directory.Delete(filefolder);
            }
            //download zip file
            Response.Redirect("batchdown.aspx?FullFilePath=" + HttpUtility.UrlEncode(zipfilename) + "&FileName=" + HttpUtility.UrlEncode(DeluxeIdentity.CurrentUser.LogOnName + ".zip") + "");//传递参数到下载压缩包的页面,下载完成后把生成的压缩包删除掉
        }

这里的创建压缩文件夹采用了ICSharpCode.SharpZipLib.Zip,其下载地址是:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

这里调用了 fastZip.CreateZip(zipfilename, filefolder, true, "");来创建压缩包,注意这里的第一个参数要写存储的文件的全路径,否则不能生成压缩文件

 

batch.CreatePackageByCompanyFileDomain(lists);
这里是根据选择的文件创建临时文件包

 

        /// <summary>
        /// 根据文件列表创建下载文件夹,并在文件夹里放置所有文件及其附件
        /// </summary>
        /// <param name="companyfiles"></param>
        public void CreatePackageByCompanyFileDomain(List<CompanyFileDomain> companyfiles)
        {
            if (companyfiles.Count > 0)
            {
                foreach (CompanyFileDomain fileEntity in companyfiles)
                {
                    CreatePackageByCompanyFileDomain(fileEntity);
                }
            }
        }
        /// <summary>
        /// 根据文件ID创建单个文件夹,并在其中放置文件及其附件
        /// </summary>
        /// <param name="resourceID"></param>
        public void CreatePackageByCompanyFileDomain(CompanyFileDomain companyfile)
        {
            if (companyfile != null)
            {
                if (!Directory.Exists(downLoadPath))
                {
                    Directory.CreateDirectory(downLoadPath);
                }
                string sourcefileNameWithoutExtend = companyfile.FileName.Substring(0,companyfile.FileName.Length-companyfile.ExtendFileName.Length);
                string sourcefileFullPath = RootFilePath + companyfile.RelativePath + "\\" + companyfile.ID + companyfile.ExtendFileName;
                string desfileFolder = downLoadPath + sourcefileNameWithoutExtend;
                string desfileattchFolder = string.Empty;

                if (Directory.Exists(desfileFolder))
                {
                    desfileFolder = RenameFolder(desfileFolder);
                }
                desfileattchFolder = desfileFolder + "http://wwwcccc.com/yungboy/admin/file://attchments//";
                Directory.CreateDirectory(desfileFolder);
                FileInfo newFile = new FileInfo(sourcefileFullPath);
                newFile.CopyTo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                FileInfo tempFile = new FileInfo(desfileFolder + "\\" + companyfile.ID + companyfile.ExtendFileName);
                tempFile.MoveTo(Path.Combine(desfileFolder, companyfile.FileName));
                //have attchement
                MaterialList materials = MaterialAdapter.Instance.LoadMaterialsByResourceID(companyfile.ID);
                if (materials.Count > 0)
                {
                    Directory.CreateDirectory(desfileattchFolder);
                    foreach (Material ma in materials)
                    {
                        string sourceAttchFullPath = RootAttchPath.Remove(RootAttchPath.Length - 1, 1);
                      
                        sourceAttchFullPath += ma.RelativeFilePath;
                        FileInfo attFile = new FileInfo(sourceAttchFullPath);
                        attFile.CopyTo(desfileattchFolder + ma.ID + attFile.Extension);
                        FileInfo tempAtt = new FileInfo(desfileattchFolder + ma.ID + attFile.Extension);
                        tempAtt.MoveTo(desfileattchFolder + ma.OriginalName);//重命名的实现
                    }
                }
            }
        }

 

posted @ 2010-09-22 19:06  碧海蓝天_C#  阅读(1318)  评论(0编辑  收藏  举报
官方QQ群:127876820【C#编程技术-全国站--未满人】