zip pdf,excel, word files and download using c#

8 posts, 0 answers
  1. Venkata
    Venkata avatar
    126 posts
    Member since:
    Nov 2009

    Posted 10 Aug Link to this post

    Hi,

    I could not find any sample project or any straight code for zip the pdf,excel, word files and download using c#.

    here what i want to do..

    i have a couple of physical files on my server(pdf,excel, word files -- mix and match).

    ex: 

    //server1/E_drive/FS3_Edrive/FS14_Hdrive/FileServer-14H/TotalCMSV6/docs/Leads/02062016/Questionaire(1042).pdf
    //server1/E_drive/WFS3_Edrive/FS14_Hdrive/FileServer-14H/TotalCMSV6/docs/Leads/02062016/Metatdata(1043).pdf
    //server1/E_drive/FS3_Edrive/FS14_Hdrive/FileServer-14H/TotalCMSV6/docs/Leads/02062016/FilledForm(1044).doc
    //server1/E_drive/WFS3_Edrive/FS14_Hdrive/FileServer-14H/TotalCMSV6/docs/Leads/02062016/Biodata(1045).xls

    i would like to zip those files and download to the user..

     

    Please provide a sample code or project.

     

    Thank you in advance

     

  2. Venkata
    Venkata avatar
    126 posts
    Member since:
    Nov 2009

    Posted 11 Aug in reply to Venkata Link to this post

    I found small piece of code but after i download, all of my files are same.

    what i am doing wrong here?

     protected void Button1_Click(object sender, EventArgs e)
            {

                MemoryStream memStream = new MemoryStream();
                ZipPackage Package = ZipPackage.Create(memStream);

                foreach (RadListBoxItem item in RadListBoxFiles.Items)
                {
                    string fileName = Path.GetFileName(item.Value.ToString());               
                    byte[] data = ReadAllBytes(item.Value.ToString());               
                   
                    Stream stream = new MemoryStream(data);               
                    Package.AddStream(stream, fileName);                
                }

                SendZipToClient(memStream, Package);

    }

     public byte[] ReadAllBytes(string fileName)
            {
                byte[] buffer = null;
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                }
                return buffer;
            }

     

    private void SendZipToClient(MemoryStream memStream, ZipPackage Package)
            {
                Package.Close(false);
                memStream.Position = 0;

                if (memStream != null && memStream.Length > 0)
                {
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=MyDocs.zip");
                    Response.ContentType = "application/zip";
                    Response.BinaryWrite(memStream.ToArray());
                    Response.End();
                }
            }

  3. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 15 Aug Link to this post

    Hi Venkata,

    I tested the provided code and couldn't find anything suspicious in the results of executing it. Could you please elaborate more on what do you mean when saying that the files are the same? Is it that their size is the same?

    In case you have troubles with packaging the files in the .zip, could you check if the proper file paths are passed and the streams are properly filled?

    Regards,
    Tanya
    Telerik by Progress

  4. Venkata
    Venkata avatar
    126 posts
    Member since:
    Nov 2009

    Posted 16 Aug in reply to Tanya Link to this post

    Hi,

    Thank you for your reply.

    I got another issue. I am trying to zip and download 500 pdf documents(344 MB) but it's taking about 4 minutes to complete the operation. Is there any way i can speed up my process?

    here is my code:

            protected void btnDownloadTelerik_Click(object sender, EventArgs e)
            {
                try
                {
                    MemoryStream objStream;
                    MemoryStream outputStream = new MemoryStream();
                    ZipPackage zipPackage = ZipPackage.Create(outputStream);

                    foreach (RadListBoxItem item in RadListBoxFiles.Items)
                    {
                        using (FileStream fileStream = File.OpenRead(item.Value.ToString()))
                        {
                            objStream = new MemoryStream();
                            objStream.SetLength(fileStream.Length);
                            fileStream.Read(objStream.GetBuffer(), 0, (int)fileStream.Length);
                        }

                        string fileName = Path.GetFileName(item.Value.ToString());
                        zipPackage.AddStream(objStream, fileName);
                    }
                    SendZipToClientNew(outputStream, zipPackage);              
                }
                catch (Exception ex)
                {
                    throw new Exception("Error While generating download files list " + ex.Message);
                }
            }

            private void SendZipToClientNew(MemoryStream memStream, ZipPackage Package)
            {
                string destFile = "DownloadFiles.zip";
                Package.Close(false);
                memStream.Position = 0;
                if (memStream != null && memStream.Length > 0)
                {
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=\"" + destFile);
                    Response.ContentType = "application/zip";
                    Response.BufferOutput = false;   // to prevent buffering
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    if (Response.IsClientConnected)
                    {
                        Response.Flush();
                    }
                    while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        Response.OutputStream.Write(buffer, 0, bytesRead);
                    }
                    Response.End();
                }
            }

  5. Venkata
    Venkata avatar
    126 posts
    Member since:
    Nov 2009

    Posted 18 Aug Link to this post

    And also my  IIS Worker Processor went up to 702 MB RAM utilizing

    after i tried multiple time i am getting System out of Memory exception.

    How to avoid?

  6. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 19 Aug Link to this post

    Hi Venkata,

    The OutOfMemoryException is caused by the fact that the zipped files are kept in a MemoryStream. Its size increases when adding the files and at some point, the available resources are not enough to continue executing the operations.

    You could change the type of the outputStream variable to FileStream and send the generated file to the user when you are done with its creation. Then, you can delete it from the server to clean up space if you don't plan to use it.

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress

  7. Venkata
    Venkata avatar
    126 posts
    Member since:
    Nov 2009

    Posted 19 Aug in reply to Tanya Link to this post

    Would you able to provide any sample code?
  8. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 24 Aug Link to this post

    Hello Venkata,

    All you need to do is to change the type of the variable. Here is how the code looks like when this is done:
    FileStream outputStream = new FileStream(Server.MapPath("result.zip"), FileMode.OpenOrCreate);

    The SendZipToClientNew() method's parameters should be updated accordingly to handle FileStream objects instead MemoryStreams.

    In regards to the deletion of the file, you could refer to the suggestions in this StackOverflow thread.

    Regards,
    Tanya
    Telerik by Progress

Back to Top