Asp tutorial. net file batch upload and download code and detailed description
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 ") +" "); // pass the parameters to the page for downloading the compressed package. After the download is complete, delete the generated compressed package.
}
Here, ICSharpCode. SharpZipLib. Zip is used to create a compressed folder, which is:
Http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
FastZip is called here. createZip (zipfilename, filefolder, true, ""); To create a compressed package. Note that the first parameter must be the full path of the stored file. Otherwise, the compressed file cannot be generated.
Batch. CreatePackageByCompanyFileDomain (lists );
Create a temporary package based on the selected file
/// <Summary>
/// Create a download folder Based on the file list and place all files and their attachments in the folder
/// </Summary>
/// <Param name = "companyfiles"> </param>
Public void CreatePackageByCompanyFileDomain (List <CompanyFileDomain> companyfiles)
{
If (companyfiles. Count> 0)
{
Foreach (CompanyFileDomain fileEntity in companyfiles)
{
CreatePackageByCompanyFileDomain (fileEntity );
}
}
}
/// <Summary>
/// Create a single folder Based on the file ID and place the file and its attachments in it
/// </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); // rename implementation
}
}
}
}