0

we are trying to save zip file using C# Ionic zip library. but it seems to be giving error that the file is not found.

System.IO.FileNotFoundException: 'Could not find file 'PhysicalPath\JobPortal\Job\DownLoadSelectedFiles'.'

the code is as under:

public ActionResult DownLoadSelectedFiles(string applicantIds)
        {
            List<ApplicantList> listapplicant = _applicantBl.GetFileNames(applicantIds); 
                    MemoryStream ms = new MemoryStream();

                    using (ZipFile zip = new ZipFile())
                    {
                        foreach (ApplicantList t in listapplicant)
                        {
//t.FileName is relative path
                            zip.AddFile(Server.MapPath(t.FileName),"CVs");
                                     
                        }

                        zip.Save(ms); // this line generates error
                    }
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms.ToArray(), "application/zip");
           }

any help appreciated

Abdul Ali
  • 1,905
  • 8
  • 28
  • 50
  • 2
    `Server.MapPath` maps a logical *directory* to the physical *directory* path. It does not map a releative file name (relative to what?) to an absolute file system path. – Klaus Gütter Dec 17 '21 at 09:51
  • i think your line Server.MapPath(t.FileName) doesn't give the value You expect. Try outputting it to the console or add a watch – T. Nielsen Dec 17 '21 at 09:58
  • @KlausGütter this is a MVC application and files are stored in root folder of the web application. as per my understanding, the first argument to zip.add is the path of the file which we want to add. kindly guide if am mistaking \ – Abdul Ali Dec 17 '21 at 10:52
  • server.mappath give C:\AppPath\JobPortal\Images\ApplicantResume\637750273214460912.pdf – Abdul Ali Dec 17 '21 at 11:51

1 Answers1

0

The issue has been resolved. seems just needed to add a check if the file physically was present or not.

if(Sytem.IO.File.FileExists(Server.MapPath(t.FileName))
{

  zip.AddFile(Server.MapPath(t.FileName),"CVs");
}
Abdul Ali
  • 1,905
  • 8
  • 28
  • 50