Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to download multiple files from Application folder and create zip file.
For that i have used checklist to show list of files that folder have .So can download only selected files and create zip for same.
following code i have tried..

What I have tried:

Code in page load to create checkboxlist as follows
if (!IsPostBack)
            {
                //DirectoryInfo dir = new DirectoryInfo(Server.MapPath(@"D:/PDFFiles/"));
                DirectoryInfo dir = new DirectoryInfo(@"D:/PDFFiles/");

                FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
                {
                    string fileName = Path.GetFileName(file.ToString());
                    ListItem item = new ListItem(fileName);
                    cblFiles.Items.Add(item);
                }
                Response.Write("<script LANGUAGE='JavaScript' >alert('" + files.Length + " Files found')</script>");
            }



code on download button click

protected void Button1_Click(object sender, EventArgs e)
        {
            if (cblFiles.SelectedItem == null)
            {
                // No options selected!
                Response.Write("<script LANGUAGE='JavaScript' >alert('You must select one or more files to download.')</script>");
                //base.DisplayAlert("You must select one or more files to download.");
                return;
            }
           
            var downloadFileName = string.Format("YourDownload-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "filename=" + downloadFileName);
     

        // Zip the contents of the selected files
        using (ZipFile zip = new ZipFile())
        {
            // Add the password protection, if specified
            if (!string.IsNullOrEmpty(txtZIPPassword.Text))
            {
                zip.Password = txtZIPPassword.Text;
                zip.Encryption = EncryptionAlgorithm.WinZipAes128;
            }      
            var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine);


            // Add the checked files to the ZIP
            foreach (ListItem li in cblFiles.Items)
                if (li.Selected)
                {
                    // Record the file that was included in readMeMessage
                    readMeMessage += string.Concat("\t* ", li.Text, Environment.NewLine);

                    // Now add the file to the ZIP (use a value of "" as the second parameter to put the files in the "root" folder)
                    zip.AddFile(li.Value, "Your Files");
                }
          
                    // Add the README.txt file to the ZIP
            zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII);


            // Send the contents of the ZIP back to the output stream
            //zip.Save(downloadFileName);
            zip.Save(Response.OutputStream);
            Response.Close();
        }
        }


but not working for me when i click on dowload button click it shows exception like
FileNotFoundException at
C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\BLWRM0032016-17.pdf":null}


Please give mi solution
thanks
Posted
Updated 16-Apr-18 11:06am

You are not adding the File path to the ListItem and trying to get the path from Value property,

try adding the path to the checkbox list Item's Value prperty
foreach (FileInfo file in files)
                {
                    string fileName = Path.GetFileName(file.ToString());
                    ListItem item = new ListItem(fileName,file.FullName); // set the path in value property 
                    cblFiles.Items.Add(item);
                }


Since the path is not specified, it will look into the machine default location and of-course the file is not present over there, which results in FileNotFoundException
 
Share this answer
 
v2
Comments
SujataJK 5-Apr-17 3:03am    
Thanks @Karthik
but iterate foreach like this
DirectoryInfo dir = new DirectoryInfo(@"D:/PDFFiles/");

FileInfo[] files = dir.GetFiles();

foreach (FileInfo file in files)
{
string fileName = Path.GetFileName(file.ToString());
ListItem item = new ListItem(fileName);
cblFiles.Items.Add(item);
}

Here files is FileInfo[] type array ND I have added directory files to files .So i have path na
Karthik_Mahalingam 5-Apr-17 3:09am    
it will have only the file name, not the full path.
SujataJK 5-Apr-17 4:23am    
yes yes u are correct.I will check your above solution
Karthik_Mahalingam 5-Apr-17 4:26am    
ok fine.
SujataJK 5-Apr-17 4:38am    
i tried but not working for me
when print file.FullName in looks like
D:SujataWebApplicationZipFileDemoZipFileDemoDownloadLibraryPBLWRM0000116-17.pdf
instead of
D:\Sujata\WebApplication\ZipFileDemo\ZipFileDemo\DownloadLibrary\PBLWRM0000116-17.pdf
Hi Karthik!

Does NOT work for me. I am getting this DataBinding error message when I tried your code. Can you please help? See my code, below.

DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name....


protected void Page_Load(object sender, EventArgs e) 
{

  if (!IsPostBack)
  {

    string[] filePaths = Directory.GetFiles(Server.MapPath("../Files/"));
    List<listitem> files = new List<listitem>(); 


    foreach (string filePath in filePaths)
    {
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        // string fileName = Path.GetFileName(files.ToString());

     }

      FileGrid.DataSource = files;
      FileGrid.DataBind(); // this line is giving me the problem.
  }

}
 
Share this answer
 
v2
Comments
Jochen Arndt 17-Apr-18 2:59am    
Please don't post questions as solutions to other questions. Use comments to reply to solutions or post a new question (which would be the better choice here because your error is sourced by code that does not exist in Karthik's answer).

However, listitem != ListItem

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