[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2711
  • Last Modified:

ASP.NET c# code: Zipping files and saving files to client local computer with save file dialog box

The following code saves the zipped file in the server.
I have Ionic.Zip.dll in the bin folder
using System.IO;
using Ionic.Zip;
protected void btnzip_Click(object sender, EventArgs e)
    {
       
string path = Server.MapPath("~/App_Info_Folders/" + lblattfld.Text + "/");
        string[] filenames = Directory.GetFiles(path);

        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(filenames, "files");
            zip.Save(Server.MapPath("~/samplefiles.zip"));

            lblmessage.Text = "ZIP File Created Successfully";

        }
      }
I want the code to be modified in such a way that the zipped file is directly saved on the client local computer through save file dialog box.
0
Srinivas Mantha
Asked:
Srinivas Mantha
1 Solution
 
Mohit VijayCommented:
What is meaning of Directly save? If you want to save on client computer without informing user, it will not be possible, because of security issues in web application. Can be done with Windows Application only.
0
 
XGISCommented:
Please check this link;

Client download samples
0
 
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
To VJsoft:
I mean save directly (without saving on server) on client computer of course with knowledge of user. Save file dialog box permits user to save on any location on the client user computer. I do not think the code is difficult at all. I am not able recall now.
To:
XGIS: client download samples are not helpful. I had seen them before approaching Experts-Exhange.
0
 
Chinmay PatelEnterprise ArchitectCommented:
after

  zip.Save(Server.MapPath("~/samplefiles.zip"));

Open in new window


put this code

string strURL=Server.MapPath("~/samplefiles.zip");
          WebClient req=new WebClient();
          HttpResponse response = HttpContext.Current.Response;
          response.Clear();
          response.ClearContent();
          response.ClearHeaders();
          response.Buffer= true;
          response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
          byte[] data=req.DownloadData(Server.MapPath(strURL));
          response.BinaryWrite(data);
          response.End();

Open in new window


Source: http://venkateswarlu.co.in/DotNet/download_file.aspx
0
 
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
Apt and complete solution. The code is working to my expectations. Only a small correction is required
Intead of
string strURL=Server.MapPath("~/samplefiles.zip");
we need to use
string strURL = "~/samplefiles.zip";
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now