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