ASP. NET file and attachment batch download Solution

Source: Internet
Author: User

Application scenarios

The document administrator can upload attachments to users when sending documents. As a common user, the administrator needs to download the documents sent to the company in batches, you must be able to download the company's documents and attachments.

Difficulties:

(1) package multiple files and their attachments together with the company documents.

(2) determine the folder and file name, because the original file name may be repeated.

(3) package folders (including files and subfolders and files in subfolders)

Solution:

(1) create a temporary folder with the same name as the current user's login name. This folder stores all the files and attachments selected for batch download. The Temporary Folder name is named after the current user's login name, for example, if the name of zhangsan is zhangsan, the folder zhangsan is created.

(2) In the zhangsan folder, create subfolders based on the number of selected company files, and create subfolders based on the number of company files selected, the name of the subfolder is named by the original name of the company file. For example, the company file is .txt 0060301company file .txt, and the file name is 4fe1c0ec-bd6c-4a76-9d49-f1745c7390db.txt.

(3) If a single company file has attachments, create an attchments folder under the subfolders to store attachments. If there are no attachments, do not create them.
(4) After creating temporary folders and files, use file. the copyto () method Copies files and attachments to the corresponding folder. Because the file is renamed during upload, after the copy is complete, you must rename the files in the Temporary Folder, file is used. move ()/file. moveTo () method.

(5) package folders to generate compressed files
(6) Click to download an object

 

Operation interface:

 

Core code:

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 = 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
}
}
}
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.