一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net文件批量上传下载代码与详细说明

时间:2022-06-25 05:36:00 编辑:袖梨 来源:一聚教程网

asp教程.net文件批量上传下载代码与详细说明
private void DownLoadCompressFile()
        {
            //create file package
            List lists = new List();
            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);
这里是根据选择的文件创建临时文件包

 

        ///


        /// 根据文件列表创建下载文件夹,并在文件夹里放置所有文件及其附件
        ///

        ///
        public void CreatePackageByCompanyFileDomain(List companyfiles)
        {
            if (companyfiles.Count > 0)
            {
                foreach (CompanyFileDomain fileEntity in companyfiles)
                {
                    CreatePackageByCompanyFileDomain(fileEntity);
                }
            }
        }
        ///
        /// 根据文件ID创建单个文件夹,并在其中放置文件及其附件
        ///

        ///
        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);//重命名的实现
                    }
                }
            }
        }

热门栏目