asp.net檔案批量上傳下載代碼與詳細說明

來源:互聯網
上載者:User

asp教程.net檔案批量上傳下載代碼與詳細說明
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);//重新命名的實現
                    }
                }
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.