Developer forum

Forum » CMS - Standard features » Download Zipped folder to browser

Download Zipped folder to browser

Kevin O''''Driscoll
Reply

Hi, I have a very frustrating issue. Im pulling .pdf files from a Server on the same network and copying them to the DW folder structure.

All connections are working fine and they are being copied. I then zip them and create a .zip, all is good, I can open the zip from the folder and the files within.

The folder gets downloaded to the browser downloads folder, but when I open it I have Windows Error:

"Windows cannot open the folder. The compressed (zipped) Folder '###' is invalid". on a mac the zip converts itself to .cpgz file and loops.

            if (files.Any())
            {
                var temp = System.Web.HttpContext.Current.Server.MapPath("~/Files/System/UserDownloads");

                // empty the temp (UserDownloads) folder
                System.IO.Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f));

                // copy the selected files to the temp (UserDownloads) folder
                files.ForEach(f => System.IO.File.Copy(f, System.IO.Path.Combine(temp, System.IO.Path.GetFileName(f))));

                List<string> fileList = new List<string>();

                while (true)
                {
                    if (files.Count() == fileList.Count())
                    {
                        break;
                    }
                    fileList = System.IO.Directory.EnumerateFiles(temp).ToList();
                }

                if (fileList.Count() == files.Count())
                {
                    string attachment = "attachment; filename=TestZipFile.zip";
                   // System.Web.HttpContext.Current.Response.Clear();
                    //System.Web.HttpContext.Current.Response.ClearHeaders();
                    //System.Web.HttpContext.Current.Response.ClearContent();

                    System.Web.HttpContext.Current.Response.ContentType = "application/zip"; 
                    //System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
                    System.Web.HttpContext.Current.Response.Charset = System.Text.Encoding.UTF8.WebName;
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
                    //System.Web.HttpContext.Current.Response.AddHeader("Pragma", "must-revalidate");
                    //System.Web.HttpContext.Current.Response.AddHeader("Cache-Control", "must-revalidate");
                    //System.Web.HttpContext.Current.Response.Write("Test;Test2;Test3");

                    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Server.MapPath("~/Files/System/UserDownloads"));

                    Dynamicweb.Content.Files.ZipFile.ZipFolder(dir, "TestZipFile.zip");

                    System.Web.HttpContext.Current.Response.TransmitFile(System.Web.HttpContext.Current.Server.MapPath("~/Files/System/UserDownloads/TestZipFile.zip"));
                    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();   // instead of Response.End()

              }

        }

Im using a standard DW web.config and my server is confd correctly (Mime Types etc)

Testing on 2 pure .net applications it works fine and testing with 2 diffrent 3rd party zip .dlls the exact same result, works fine in vanilla and not in DW.

Is DW doing something wild wth the HttpContext here? Is there a setting in DW / web.config I dont know about?

Rgds

Kev

 


Replies

 
Nicolai Pedersen
Reply

Hi Kevin

I cannot see from your code how you execute this?

I can see you tried with System.Web.HttpContext.Current.Response.Clear(); - if that does not do the trick, I cannot see how DW should have an impact on this...

BR Nicolai

 
Kevin O''''Driscoll
Reply

Hi Nicolai,

I create the zip folder:

Dynamicweb.Content.Files.ZipFile.ZipFolder(dir, "TestZipFile.zip");

It gets created in the folder where the files where copied, the files are in the zip folder, you can open them as normal.

I send the zipped folder down to the browser on the response:

System.Web.HttpContext.Current.Response.TransmitFile(System.Web.HttpContext.Current.Server.MapPath("~/Files/System/UserDownloads/TestZipFile.zip"));

It is created in the browsers download folder as normal. The zip folder can be opened but with windows void error, showing no files.

As you say I tried to clear everything first but it still fails to open correctly in the users download folder. Its like it gets corrupted on the Transfer.

I thought because DW fiddles with the HttpContext??? Because vanilla .net works fine

How about DW Compression settings vs Server compression settings? would this affect the .zip?

 
Nicolai Pedersen
Reply

If you are in the context of a pageview, it could be compression - you can disable compression from the MC. If you are not in the context of the pageview, it cannot be DW compression. But it could be IIS settings or web.config if you are using an old Dynamicweb installation (or old web.config). Back in the old days, web.config contained compression settings.

 
Kevin O''''Driscoll
Reply

Hi Nicoli, I guessed it could be compression settings in DW. (Dynamicweb.dll 8.9.1.7) The MC is set to gzip for the whole site and in web.config you have

 <add key="HttpCompressionSettings" value="/Admin/Public,/Admin/Statisticsv2,/Admin/FileManager,/Admin/Module/Ecom/,/Admin/Module/Ipaper,/Admin/Module/News/News_Module_List.aspx,/WebResource.axd" />
    <add key="HttpCompressionForceSettings" value="/Admin/Public/eCom/InstantSearch.ashx" />
    <add key="HttpCompressionPreferredAlgorithm" value="deflate" />
    <add key="HttpCompressionLevel" value="low" />

Which I think overrides for specific folders so Ill try fiddling with these an see if I can find an outcome (Just back from holiday)

 
Nicolai Pedersen
Reply

Hi Kevin

Just remove this section as it is deprecated.

BR Nicolai

 

You must be logged in to post in the forum