Solved

Force Download of Zip file via JQuery

Posted on 2016-07-26
2
99 Views
Last Modified: 2016-09-06
I am trying to create a download of a zip file using JQuery, however everything I try just opens the zip file rather than offer it as a download in IE11.

I have to work with IE11 as that's the corporate standard.

this is the code im working with right now.

 $.ajax({
                    type: "POST",
                    url: "SubmissionTracker.aspx/ExportFile",
                    data: JSON.stringify(params),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        
                        $("#hiddenLink").attr("href", data.d);
                        console.log($("#hiddenLink").val());
                        $("#hiddenLink")[0].click();
                        $("." + excel).show();
                        $("." + loader).hide();

                    },
                    error: function (data) {
                        console.log("error :" + data);
                        console.log(data);
                        $("." + excel).show();
                        $("." + loader).hide();
                    }
                });

Open in new window


I have tried this both ways too

 <a id="hiddenLink" href="" download="Export.zip"></a>

Open in new window

and
 <a id="hiddenLink" href=""></a>

Open in new window


any ideas on how to resolve this are most welcome
0
Comment
Question by:SimonPrice3376
  • 2
2 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41729336
I don't think trigger click works the way you would expect when applied to an <a> element - I am assuming you want this to start a download.

Rather do this on line 9
window.location.href = data.d

Open in new window


This will redirect the page to the zip file - but because it is a download the page won't navigate away.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41748702
This sample code might be of assistance from this question
      $scope.getFile = function () {
        $http.get(
          't1469.php',
          { 
            responseType: 'arraybuffer' 
          }
        ).then(function successCallBack(response) {    
          var octetStreamMime = 'application/octet-stream';
          // Get the headers
          var headers = response.headers();
          // Get the filename from the header.
          var contentDisposition = headers['content-disposition'];
          var fileName = contentDisposition.substring(21, contentDisposition.length);
          // Determine the content type from the header or default to "application/octet-stream"
          var contentType = headers['content-type'] || octetStreamMime;
          try {
            var file = new Blob([response.data], { type: contentType });
            if (navigator.msSaveBlob) {
              navigator.msSaveBlob(file, fileName);
            }
            else {
              alert('Your browser does not support msSaveBlob. Try Internet Explorer");
            }
          }
          catch (ex) {
          }
        // ADDED MISSING '}'
        },
        function errorCallBack(response) {
          console.log(response);

          var modalOptions;
          modalOptions = {
            includeCloseButton: true,
            closeButtonText: 'Close',
            headerText: 'Error Message',
            bodyText: 'An error occured while retrieving the file.  Please review the error log for details.'
          };
          modalMessageService.showModal({}, modalOptions);
        });
      }    

Open in new window

There is a working sample here

The code will only work on IE - it uses the msSaveBlob call
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question