using ionic.zip;
public string SaveStreamToFile()
{
CV.Programs.Entity.ParticipantDocument participantDocument = new CV.Programs.Entity.ParticipantDocument();
participantDocument.ParticipantId = Convert.ToInt32(Session["ParticipantId"]);
ParticipantDocumentBLL participantDocumentBLL = new ParticipantDocumentBLL();
DataTable _dataTable=participantDocumentBLL.GetParticipantDocument(participantDocument);
string directoryPath = Server.MapPath("~/abc/ParticipantDOC/") +"admin"+ Session["ParticipantId"].ToString() + "" + DateTime.Now.ToString("ss");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
ViewState["DirName"] = directoryPath;
}
for (int i = 0; i < _dataTable.Rows.Count; i++)
{
if (Convert.ToString(_dataTable.Rows[i]["UploadedDocumentName"]) != "")
{
byte[] file = null;
file = _dataTable.Rows[i]["UploadedDocument"] as byte[];
string filename = _dataTable.Rows[i]["UploadedDocumentName"].ToString();
string strExtenstion = Path.GetExtension(filename);
// Create a FileStream object to write a stream to a file
using (FileStream fileStream = System.IO.File.Create(directoryPath+"/" + Session["ParticipantId"] + "_"+i+"Document" + strExtenstion))
{
fileStream.Write(file, 0, file.Length);
}
}
}
return directoryPath;
}
protected void DownloadAllDocuments(string dirPath)
{
using (MemoryStream ms = new MemoryStream())
{
// create new ZIP archive within prepared MemoryStream
//Directory.CreateDirectory(Server.MapPath("/KIZUNA/ParticipantPDF/" + Session["UserLoginId"].ToString() + "_" + Session["ParticipantId"] + ".zip"));
//ZipFile zip =ZipFile.Create(Server.MapPath("/ abc /ParticipantPDF/"+Session["UserLoginId"].ToString()+"_"+Session["ParticipantId"]+".zip"));
// add some files to ZIP archive
Response.Clear();
Response.AddHeader("content-disposition", "filename=sample.zip");
Response.ContentType = MimeType(".zip");
//string[] allFiles = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(dirPath);
zip.Save(Response.OutputStream);
}
//zip.BeginUpdate();
//for (int i = 0; i < allFiles.Length; i++)
//{
// zip.Add(allFiles[i].ToString());
//}
//ms.WriteTo(Response.OutputStream);
//// clear response stream and set the response header and content type
//// write content of the MemoryStream (created ZIP archive) to the response stream
//zip.CommitUpdate();
//zip.Close();
}
if (Directory.Exists(dirPath))
{
Directory.Delete(dirPath, true);
//Directory.CreateDirectory(directoryPath);
//ViewState["DirName"] = directoryPath;
}
if (Directory.Exists(Server.MapPath("/ abc /ParticipantPDF/" + Session["UserLoginId"].ToString() + "_" + Session["ParticipantId"] + ".zip")))
{
Directory.Delete(Server.MapPath("/ abc /ParticipantPDF/" + Session["UserLoginId"].ToString() + "_" + Session["ParticipantId"] + ".zip"), true);
//Directory.CreateDirectory(directoryPath);
//ViewState["DirName"] = directoryPath;
}
// close the current HTTP response and stop executing this page
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
No comments:
Post a Comment