1

On click of a link, I am trying to download a pdf document. For simplicity, the code shown below is downloading an image. I have referred the example as seen in http://jsfiddle.net/Qjvb3/. Inside the click event handler of the link, I am trying to do the same thing and it works.

   jQuery(document.body).on('click', '.downLoadLink', function (event) {
   }

But when I try to do the same thing inside an ajax call, the image is not getting downloaded.

   ds.downloadDxRecordForm = function (fileName,downloadAnchorElement,pdfFile) {
        return Q.when($.ajax({
            type: "GET",
            url: config.nccsysWebApiEndpoint + 'DxRecordForm/DownloadDxRecordForm',
            data: {
                fileName: fileName
            },
            contentType:'application/pdf',
            beforeSend: function (jqXhr) {
                ds.setXhrRequestHeader(jqXhr);
                jqXhr.setRequestHeader('Accept', 'application/pdf');
            }
        }).then(function (data) {
           //I am placing the jsfiddle code here
           //$(downloadAnchorElement).attr("href",.....).attr("download","..")
        }).fail(function (promise) {
            return ds.ajaxFailed(promise,
                {

                });
        }));

    };
};

To this ajax call I am passing the anchor element and then trying to set the href and download attributes when the ajax call is completed. In the console window I see that attributes are getting added to the anchor element but somehow the image is not getting downloaded.

user2585299
  • 873
  • 2
  • 19
  • 41
  • These are two very different things. One is updating HTML attributes on a link that tell the browser to download it as a file. The other is getting the contents of a file via an AJAX request. Is there a reason you're not just coding the link in HTML? e.g. `link` – Tom Pietrosanti Apr 16 '14 at 19:07
  • This may help you: http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Tom Pietrosanti Apr 16 '14 at 19:09
  • I am yet not downloading the pdf file. I am writing the same code that downloads an image at both the places. The image should get downloaded inside the ajax call as well because it gets the anchor element as a parameter and then the attributes are set. – user2585299 Apr 16 '14 at 19:22
  • Oh, I see. So you're creating the link and setting the `href` to the base64 encoded url data of the image that you get from the ajax call. You're also setting the download attribute on this link. Is that correct? – Tom Pietrosanti Apr 16 '14 at 19:53
  • Yes, you are right. In my actual example, the ajax will give me the pdf not the image. For testing purpose I was working with the image initially. If that works I will check for pdf. – user2585299 Apr 16 '14 at 20:10
  • Also wanted to add that the ajax call is in different file. – user2585299 Apr 16 '14 at 20:57
  • Have you checked the console for errors? One possibility I can think of would be same origin policy, which would show up in the console. – Tom Pietrosanti Apr 17 '14 at 11:59
  • Did you ever get this working? – Mawg says reinstate Monica Jan 16 '17 at 22:35

0 Answers0