3 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 02 Jul 2013, 10:50 AM
Hi Babak,
You can use the ZipFile class to achieve your requirement. Please have a look at the following code I tried which works fine as expected. I am uploading few image files to the Target folder and using the ZipFile class I am creating the .zip file and finally discarding the files uploaded in the Target folder. For this to work you need the .NetFramework 4.5 version installed on your machine and need to refer the System.IO.Compression.FileSystem DLL. If you cant find the reference you can download it from here.
ASPX:
C#:
Thanks,
Shinu.
You can use the ZipFile class to achieve your requirement. Please have a look at the following code I tried which works fine as expected. I am uploading few image files to the Target folder and using the ZipFile class I am creating the .zip file and finally discarding the files uploaded in the Target folder. For this to work you need the .NetFramework 4.5 version installed on your machine and need to refer the System.IO.Compression.FileSystem DLL. If you cant find the reference you can download it from here.
ASPX:
<
telerik:RadAsyncUpload
runat
=
"server"
ID
=
"AsyncUpload_Attachment"
TargetFolder
=
"~/Images/Img/"
OnFileUploaded
=
"AsyncUpload_Attachment_FileUploaded"
PostbackTriggers
=
"RadButton1"
InitialFileInputsCount
=
"1"
MaxFileSize
=
"10240000"
MultipleFileSelection
=
"Automatic"
/>
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
Text
=
"Upload and Generate Zip"
OnClick
=
"RadButton1_Click"
/>
C#:
protected
void
AsyncUpload_Attachment_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
string
targetfolder = AsyncUpload_Attachment.TargetFolder;
e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName));
}
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
string
uploadTarget = AsyncUpload_Attachment.TargetFolder;
string
startPath = Server.MapPath(uploadTarget);
//source path to create zip files.
string
zipPath = Path.Combine(Server.MapPath(
"~/Images/FinalUpload"
),
"User1.zip"
);
//Setting path and filename of .zip to be created.
ZipFile.CreateFromDirectory(startPath, zipPath);
//creating the .zip
Array.ForEach(Directory.GetFiles(Server.MapPath(uploadTarget)),
delegate
(
string
f) { File.Delete(f); });
//deleting the uploaded files after creating .zip file
}
Thanks,
Shinu.
0
0

Shinu
Top achievements
Rank 2
answered on 03 Jul 2013, 05:13 AM
Hi Babak,
One suggestion is at the time of uploading files you can create separate folder for individual users inside the target folder. Suppose the target folder name is InitialUploads, then when a user (say User1) tries to upload files, that can be stored inside a folder named User1 inside the Target folder "InitialUploads" and after creating the .zip you can delete this User1 folder.
Please have a look at the following sample code based on the mark-up I provided. When the user login some particular identity for ex. Username will be saved in the session and that you can use while uploading files as follows.
C#:
Thanks,
Shinu.
One suggestion is at the time of uploading files you can create separate folder for individual users inside the target folder. Suppose the target folder name is InitialUploads, then when a user (say User1) tries to upload files, that can be stored inside a folder named User1 inside the Target folder "InitialUploads" and after creating the .zip you can delete this User1 folder.
Please have a look at the following sample code based on the mark-up I provided. When the user login some particular identity for ex. Username will be saved in the session and that you can use while uploading files as follows.
C#:
protected
void
AsyncUpload_Attachment_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
string
targetfolder = AsyncUpload_Attachment.TargetFolder+Convert.ToString(Session[
"Username"
]);
if
(!System.IO.Directory.Exists(Server.MapPath(targetfolder)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(targetfolder));
//Creating user folder if it does not exists in Target folder.
}
e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName));
}
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
string
uploadTarget = AsyncUpload_Attachment.TargetFolder + Convert.ToString(Session[
"Username"
]);
string
startPath = Server.MapPath(uploadTarget);
//source path to create zip files.
string
zipPath = Path.Combine(Server.MapPath(
"~/Images/FinalUpload"
), Convert.ToString(Session[
"Username"
])+
".zip"
);
//Setting path and filename of .zip to be created.
ZipFile.CreateFromDirectory(startPath, zipPath);
//creating the .zip
System.IO.Directory.Delete(startPath,
true
);
//deleting the user folder in Target folder after creating .zip file
}
Thanks,
Shinu.