ASP.Net檔案及附件批量下載解決方案

來源:互聯網
上載者:User

應用情境

發文管理員傳送檔案給使用者,在發文的過程中,可以上傳附件;作為普通使用者,需要對收到的公司發文批量下載,下載要求能夠下載公司發文的檔案和附件。

問題痛點:

(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);//重新命名的實現
                    }
                }
            }
        }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.