Solved

Force Download of Zip file via JQuery

Posted on 2016-07-26
2
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 58

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 58

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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

630 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