Link to home
Start Free TrialLog in
Avatar of SimonPrice3376
SimonPrice3376

asked on

Force Download of Zip file via JQuery

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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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