I have used DotNetZip Lib to create zip. Currently my code creates zip folder with files. Files are jpeg files. When i extract the zip
and try to open the file. File does not open. It looks like file is corrupted or formatting hasnot been done right.
What is wrong in my below code?
[HttpPost]
[ValidateAntiForgeryTokenAjax]
public ActionResult Generatezip(List imageList)
{
string imgRootPath = ConfigurationManager.AppSettings["filepath"] + "\\";
string imgFile = string.Empty;
string imgKey = string.Empty;
string fileName = "DaysImage_";
try
{
var ms = new MemoryStream();
[ValidateAntiForgeryTokenAjax]
public ActionResult Generatezip(List
{
string imgRootPath = ConfigurationManager.AppSettings["filepath"] + "\\";
string imgFile = string.Empty;
string imgKey = string.Empty;
string fileName = "DaysImage_";
try
{
var ms = new MemoryStream();
if (imageList.Count > 0)
{
string[] fileNames = imageList.FirstOrDefault().ToString().ToLower().Split('\\');
fileName += fileNames[0] + fileNames[1] + fileNames[2] + ".zip";
{
string[] fileNames = imageList.FirstOrDefault().ToString().ToLower().Split('\\');
fileName += fileNames[0] + fileNames[1] + fileNames[2] + ".zip";
using (ZipFile zipFile = new ZipFile())
{
foreach (var imgPath in imageList)
{
imgKey = imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
imgFile = imgRootPath + imgPath;
zipFile.AddEntry(imgKey, imgFile);
//zipFile.AddFile(imgFile);
}
zipFile.Save(ms);
TempData[fileName] = ms;
// zipFile.Save(ms);
}
}
{
foreach (var imgPath in imageList)
{
imgKey = imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
imgFile = imgRootPath + imgPath;
zipFile.AddEntry(imgKey, imgFile);
//zipFile.AddFile(imgFile);
}
zipFile.Save(ms);
TempData[fileName] = ms;
// zipFile.Save(ms);
}
}
ms.Position = 0;
return Json(new { success = true, fileName }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Logger.LogError(ex);
return Json(new { success = false, fileName }, JsonRequestBehavior.AllowGet);
}
}
public ActionResult DownloadImageZip(string fileName)
{
var ms = TempData[fileName] as MemoryStream;
if (ms == null)
return new EmptyResult();
TempData[fileName] = null;
return File(ms, "application/zip", fileName);
}
}
return Json(new { success = true, fileName }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Logger.LogError(ex);
return Json(new { success = false, fileName }, JsonRequestBehavior.AllowGet);
}
}
public ActionResult DownloadImageZip(string fileName)
{
var ms = TempData[fileName] as MemoryStream;
if (ms == null)
return new EmptyResult();
TempData[fileName] = null;
return File(ms, "application/zip", fileName);
}
}
Istudent RanaPosted Feb 21, 2018, 3:09 PM
zipFile.AddEntry(imgKey, imageToByteArray(imgRootPath + imgPath));
Amit GuptaPosted Feb 21, 2018, 9:20 AM
Amit GuptaPosted Feb 21, 2018, 8:42 AM
Istudent RanaPosted Feb 21, 2018, 8:29 AM
Amit GuptaPosted Feb 21, 2018, 8:07 AM
zipFile.AddEntry(imgKey, imgFile);
Istudent RanaPosted Feb 21, 2018, 7:56 AM
Amit GuptaPosted Feb 21, 2018, 12:17 AM