0

I have jqgrid in my MVC view, which displays the number of files available. User can download the single file or multiple files in zip format after selecting the checkbox in each row of jqGrid. Single file download is working fine and able to prompt to user to save the file but when I download the files in zip format, i don't get any save prompt to save the file on client machine. Zip file is creating fine in the folder but not prompting to save. I don't know where I am doing wrong. Please see the following code and help me on this issue....

    **Controller**
    [HttpGet]
    public ActionResult DownloadZip(String[] filesToZip)
    {
        //checking if there any file to zip
        if (filesToZip == null || filesToZip.Length < 1 || filesToZip.Count() == 0)
            return (new EmptyResult());

        //creating dynamic zip file name
        var downloadFileName = string.Format("Test_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));

        //zipping the files
        using (ZipOutputStream zipos = new ZipOutputStream(System.IO.File.Create(Path.Combine(Server.MapPath(uploadLocation), downloadFileName))))
        {
            // 0-9, 9 being the highest compression
            zipos.SetLevel(9);

            //temp buffer to hold 4gb data max
            byte[] buffer = new byte[4096];

            foreach (string file in filesToZip)
            {
                ZipEntry newEntry = new ZipEntry(Path.GetFileName(file));
                zipos.PutNextEntry(newEntry);

                using (FileStream fs = System.IO.File.OpenRead(file))
                {
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        zipos.Write(buffer, 0, sourceBytes);
                    }
                    while (sourceBytes > 0);
                }
            }//end files loop
        }


        System.IO.FileInfo zipFile = new System.IO.FileInfo(Path.Combine(Server.MapPath(uploadLocation), downloadFileName));

        // clear the current output content from the buffer
        Response.Clear();

        // add the header that specifies the default filename for the 
        // Download/SaveAs dialog 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFileName);

        // add the header that specifies the file size, so that the browser
        // can show the download progress
        Response.AddHeader("Content-Length", zipFile.Length.ToString());

        // specify that the response is a stream that cannot be read by the
        // client and must be downloaded
        Response.ContentType = "application/zip";
        // send the file stream to the client
        Response.WriteFile(zipFile.FullName);


        //ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
        //return File(Path.Combine(Server.MapPath(uploadLocation), downloadFileName), "application/zip", downloadFileName);

        return (new EmptyResult());
   }

   **View**

            $("#btnDownloadZip").click(function () {

                var files = $("#list").jqGrid('getGridParam', 'selarrrow').toString();

                if (files == '') {
                    alert('Please select a file to download...');
                    return;
                }

                var fileList = files.split(",");

                $.post('<%= Url.Action("DownloadZip") %>', { filesToZip: fileList }, handleSuccess);

                //$.getJSON("/Home/DownloadZip/", { filesToZip: fileList });
            });
     function handleSuccess()
     {
     }
tereško
  • 58,060
  • 25
  • 98
  • 150
user853320
  • 19
  • 4

1 Answers1

0

Asp.Net MVC have a function name File you can use that for returning files from controller action.

See this question that shows how to return a file

Community
  • 1
  • 1
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
  • If you see my code, I have commented 3rd line from bottom. I tried to use File but did not work in my case so I tried in different way. File is working fine when i download single fine but when i download zip file then its not working.... – user853320 Mar 22 '12 at 21:24
  • $("#btnDownloadZip").click(function () { var files = $("#list").jqGrid('getGridParam', 'selarrrow').toString(); if (files == '') { alert('Please select a file to download...'); return; } var fileList = files.split(","); $.post('<%= Url.Action("DownloadZip") %>', { filesToZip: fileList }, handleSuccess); //$.getJSON("/Home/DownloadZip/", { filesToZip: fileList }); }); – user853320 Mar 23 '12 at 00:00
  • Hi, I solved it. I used window.location = "/Home/DownloadZip?filesToZip=" + fileList; rather than Url.Action or getJSON. thx.... – user853320 Mar 23 '12 at 00:43