Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me in my project i have some image files,video files.these files are shown in gridview and these file path and name is stored in database. one button called "Download All". When user clicks on this button these file should go in zip folder and download these folder in to his system. just like we download some application then it shows zip folder and it contains files.
Posted
Updated 29-Jun-12 20:39pm
v6
Comments
Tim Corey 29-Jun-12 8:27am    
Glad to hear it. So what is the issue? What have you tried? Where are you stuck (saying "I'm stuck on how to do this" doesn't count)?
_Tushar Patil 29-Jun-12 8:39am    
Be Specific When Asking Question.....!
AmitGajjar 29-Jun-12 8:41am    
so what is the problem ???? you are not able to download files ? what error ? what is the error message ? or let us know about your confusion... otherwise we will get confused :D
Vaishali P. Patil 29-Jun-12 8:46am    
if suppose i have one movie's all song and bellow is the link download all above then how can we do this functionality
Vaishali P. Patil 29-Jun-12 8:52am    
i am trying bellow code
if (ds.Tables[0].Rows.Count > 0)
{
// Create the ZIP file that will be downloaded. Need to name the file something unique ...
string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("1/") + strNow + ".zip"));
zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression

// Loop through the dataset to fill the zip file
foreach (DataRow dr in ds.Tables[0].Rows)
{


ZipEntry zipEntry = new ZipEntry(dr["filename"].ToString());
zipOS.PutNextEntry(zipEntry);


}

zipOS.Finish();
zipOS.Close();

FileInfo file = new FileInfo(Server.MapPath("1/") + strNow + ".zip");
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/zip";
Response.WriteFile(file.FullName);
Response.Flush();
file.Delete();
Response.End();
}
}


but it only creates zip folder

 
Share this answer
 
Comments
Vaishali P. Patil 29-Jun-12 9:10am    
thanks.i used your code as

if (ds.Tables[0].Rows.Count > 0)
{



byte[] buffer = new byte[4096];

// the path on the server where the temp file will be created!
string tempFileName = Server.MapPath(@"1/" + Guid.NewGuid().ToString() + ".zip");

ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
string filePath = String.Empty;
string fileName = String.Empty;
int readBytes = 0;

foreach (DataRow dr in ds.Tables[0].Rows)
{
//var isChecked = (row.FindControl("chkSelect") as CheckBox).Checked;
//if (!isChecked) continue;
filePath = Convert.ToString(dr["EvidencePath"]);
//filePath = (row.FindControl("lblFilePath") as Label).Text;
//fileName = (row.FindControl("lblFileName") as Label).Text;
fileName = Convert.ToString(dr["FilesName"]);


ZipEntry zipEntry = new ZipEntry(fileName);

zipOutputStream.PutNextEntry(zipEntry);

using (File fs = File.OpenRead(filePath))
{
do
{
readBytes = fs.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, readBytes);

} while (readBytes > 0);
}
}

if (zipOutputStream.Length == 0)
{
//lblMessage.Text = "Please select at least one file!";
return;
}


zipOutputStream.Finish();
zipOutputStream.Close();

Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "attachment; filename=YourFile.zip");
Response.WriteFile(tempFileName);

Response.Flush();
Response.Close();

// delete the temp file
if (File.Exists(tempFileName))
File.Delete(tempFileName);
//}

}

it shows me error: Cannot declare a variable of static type 'System.IO.File'
Hi,

check this[^] link,

this may help you

thanks
-Amit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900