Download and open PDF file using Ajax
Asked Answered
S

18

115

I have an action class that generates a PDF. The contentType is set appropriately.

public class MyAction extends ActionSupport 
{
   public String execute() {
    ...
    ...
    File report = signedPdfExporter.generateReport(xyzData, props);

    inputStream = new FileInputStream(report);
    contentDisposition = "attachment=\"" + report.getName() + "\"";
    contentType = "application/pdf";
    return SUCCESS;
   }
}

I call this action through an Ajax call. I don't know the way to deliver this stream to browser. I tried a few things but nothing worked.

$.ajax({
    type: "POST",
    url: url,
    data: wireIdList,
    cache: false,
    success: function(response)
    {
        alert('got response');
        window.open(response);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) 
    {
        alert('Error occurred while opening fax template' 
              + getAjaxErrorString(textStatus, errorThrown));
    }
});

The above gives the error:

Your browser sent a request that this server could not understand.

Soulier answered 4/1, 2010 at 13:42 Comment(0)
H
47

You don't necessarily need Ajax for this. Just an <a> link is enough if you set the content-disposition to attachment in the server side code. This way the parent page will just stay open, if that was your major concern (why would you unnecessarily have chosen Ajax for this otherwise?). Besides, there is no way to handle this nicely acynchronously. PDF is not character data. It's binary data. You can't do stuff like $(element).load(). You want to use completely new request for this. For that <a href="pdfservlet/filename.pdf">pdf</a> is perfectly suitable.

To assist you more with the server side code, you'll need to tell more about the language used and post an excerpt of the code attempts.

Hattie answered 4/1, 2010 at 13:45 Comment(15)
content-disposition to attachment is not working with Ajax. I can't give a hyperlink since the document is being dynamically generated on server side.Soulier
Once again: you do not need Ajax for this. It's only asking for trouble. PDF is binary data, not character data like HTML or JSON.Hattie
var url = contextPath + "/xyz/blahBlah.action"; url += url + "?" + params; try { var child = window.open(url); child.focus(); } catch (e) { }Soulier
In some browsers the window.open will stay open and blank, which may be annoying for endusers. So, also do NOT use window.open for this. If the content-disposition is set to attachment, you will just get a Save as dialogue. The parent page will stay unchanged. Just <a href="pdfservlet/filename.pdf">pdf</a> or a <form action="pdfservlet/filename.pdf"><input type="submit"></form> is more than enough.Hattie
I have a button, onclick event I decide if I should make an ajax call to do some action XYZ or show the pdf. I tried window.location.href but it didn't work in firefix. any idea what is the right way to do this?Soulier
Just thank you for this solution! I have a form that is submitted by default using AJAX, and I needed to add a button to export the same results as PDF. So I ended up writing a function (onclick handler) that gets serialized parameters from the form and using plain simple window.location.href=urlToPdf sends a request. Thanks again!Religious
window.location works great, but what happens while the user is wating for the download? Nothing. An ajax solution would be ideal to tie in a loading bar or, at the very least, disable the download request control.Rhododendron
@Hattie I did as you said <a target="_blank" href="pdfservlet/filename.pdf">pdf</a> in IE10. But I get %PDF-1.4 . How can I add any header or content-disposition in my jsp ?Lucite
There is a limited Url length. And the author is asking about POST.Trinatrinal
If you user Bearer Authentication instead of Cookie authentication you will need to use Ajax in order to add the request headers, because you can't add those headers to a anchor tag request.Spectroheliograph
Agree with @EdwardOlamisan, this is not a correct answer as the author was trying to POST data.Medovich
This doesnt actually answer the question.Waxman
@Shayne: indeed, it only answers a much better way.Hattie
@Hattie Can you help me with this? - #58317488Linhliniment
What if I don't want to redirect a user? only want to open that PDF file on part of the page?Bonaventura
S
139

Here is how I got this working

$.ajax({
  url: '<URL_TO_FILE>',
  success: function(data) {
    var blob=new Blob([data]);
    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="<FILENAME_TO_SAVE_WITH_EXTENSION>";
    link.click();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Updated answer using download.js

$.ajax({
  url: '<URL_TO_FILE>',
  success: download.bind(true, "<FILENAME_TO_SAVE_WITH_EXTENSION>", "<FILE_MIME_TYPE>")
});
Schnauzer answered 19/12, 2014 at 10:22 Comment(12)
How can I get this behavior in IE11?Masaryk
Does it work on chrome ? I can only see a blank pdf.Clava
Yes, it does work on all modern browsers. If you see a blank pdf, try running the ajax url in a new tab. If you get a blank screen there as well, might be a problem with the pdf itself. If you do see a pdf file there and not in the downloaded file, do let me know on my email. :)Schnauzer
you made my day :) !Schistosome
Glad to know that!Schnauzer
This (anchor element) actually did not work for me on IE 11, Edge and Firefox. changing the success to just using "window.open(URL.createObjectURL(blob))" did work.Sleuth
pdf file is downloaded but no content is availble. i have save byte[] at server side and pdf content is availble. plz suggest.Reclaim
blank pdf file is downloaded.Folkrock
Blank PDF for me too. I tried one of the above approaches and it worked so I know its not my file generation server side.Telegony
To fix blank PDF: 1) Add responseType: 'arraybuffer' to the ajax call params. 2) Add {type: 'application/pdf'} as the second param to new Blob()Geostrophic
For anyone who get blank PDF like me, please read more on this answer https://mcmap.net/q/41994/-download-and-open-pdf-file-using-ajax or linked to this answer https://mcmap.net/q/42028/-download-pdf-file-using-jquery-ajaxLibratory
I was able to fix the blank PDF issue by putting in xhrFields: { responseType: 'blob' } in the parameters to $.ajax().Epigene
H
47

You don't necessarily need Ajax for this. Just an <a> link is enough if you set the content-disposition to attachment in the server side code. This way the parent page will just stay open, if that was your major concern (why would you unnecessarily have chosen Ajax for this otherwise?). Besides, there is no way to handle this nicely acynchronously. PDF is not character data. It's binary data. You can't do stuff like $(element).load(). You want to use completely new request for this. For that <a href="pdfservlet/filename.pdf">pdf</a> is perfectly suitable.

To assist you more with the server side code, you'll need to tell more about the language used and post an excerpt of the code attempts.

Hattie answered 4/1, 2010 at 13:45 Comment(15)
content-disposition to attachment is not working with Ajax. I can't give a hyperlink since the document is being dynamically generated on server side.Soulier
Once again: you do not need Ajax for this. It's only asking for trouble. PDF is binary data, not character data like HTML or JSON.Hattie
var url = contextPath + "/xyz/blahBlah.action"; url += url + "?" + params; try { var child = window.open(url); child.focus(); } catch (e) { }Soulier
In some browsers the window.open will stay open and blank, which may be annoying for endusers. So, also do NOT use window.open for this. If the content-disposition is set to attachment, you will just get a Save as dialogue. The parent page will stay unchanged. Just <a href="pdfservlet/filename.pdf">pdf</a> or a <form action="pdfservlet/filename.pdf"><input type="submit"></form> is more than enough.Hattie
I have a button, onclick event I decide if I should make an ajax call to do some action XYZ or show the pdf. I tried window.location.href but it didn't work in firefix. any idea what is the right way to do this?Soulier
Just thank you for this solution! I have a form that is submitted by default using AJAX, and I needed to add a button to export the same results as PDF. So I ended up writing a function (onclick handler) that gets serialized parameters from the form and using plain simple window.location.href=urlToPdf sends a request. Thanks again!Religious
window.location works great, but what happens while the user is wating for the download? Nothing. An ajax solution would be ideal to tie in a loading bar or, at the very least, disable the download request control.Rhododendron
@Hattie I did as you said <a target="_blank" href="pdfservlet/filename.pdf">pdf</a> in IE10. But I get %PDF-1.4 . How can I add any header or content-disposition in my jsp ?Lucite
There is a limited Url length. And the author is asking about POST.Trinatrinal
If you user Bearer Authentication instead of Cookie authentication you will need to use Ajax in order to add the request headers, because you can't add those headers to a anchor tag request.Spectroheliograph
Agree with @EdwardOlamisan, this is not a correct answer as the author was trying to POST data.Medovich
This doesnt actually answer the question.Waxman
@Shayne: indeed, it only answers a much better way.Hattie
@Hattie Can you help me with this? - #58317488Linhliniment
What if I don't want to redirect a user? only want to open that PDF file on part of the page?Bonaventura
I
34

I don't really think that any of the past answers spotted out the problem of the original poster. They all presume a GET request while the poster was trying to POST data and get a download in response.

In the course of searching for any better answer we found this jQuery Plugin for Requesting Ajax-like File Downloads (if link is broken sometime in the future, see the internet archive).

In its "heart" it creates a "temporary" HTML form containing the given data as input fields. This form is appended to the document and posted to the desired URL. Right after that the form is removed again:

jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
    .appendTo('body').submit().remove()

Update Mayur's answer looks pretty promising and very simple in comparison to the jQuery plug-in I referred to.

Implode answered 24/2, 2012 at 14:1 Comment(0)
L
9

This is how i solve this issue.
The answer of Jonathan Amend on this post helped me a lot.
The example below is simplified.

For more details, the above source code is able to download a file using a JQuery Ajax request (GET, POST, PUT etc). It, also, helps to upload parameters as JSON and to change the content type to application/json (my default).

The html source:

<form method="POST">
    <input type="text" name="startDate"/>
    <input type="text" name="endDate"/>
    <input type="text" name="startDate"/>
    <select name="reportTimeDetail">
        <option value="1">1</option>
    </select>
    <button type="submit"> Submit</button>
</form>  

A simple form with two input text, one select and a button element.

The javascript page source:

<script type="text/javascript" src="JQuery 1.11.0 link"></script>
<script type="text/javascript">
    // File Download on form submition.
    $(document).on("ready", function(){
        $("form button").on("click", function (event) {
            event.stopPropagation(); // Do not propagate the event.

            // Create an object that will manage to download the file.
            new AjaxDownloadFile({
                url: "url that returns a file",
                data: JSON.stringify($("form").serializeObject())
            });

            return false; // Do not submit the form.
        });
    });
</script>  

A simple event on button click. It creates an AjaxDownloadFile object. The AjaxDownloadFile class source is below.

The AjaxDownloadFile class source:

var AjaxDownloadFile = function (configurationSettings) {
    // Standard settings.
    this.settings = {
        // JQuery AJAX default attributes.
        url: "",
        type: "POST",
        headers: {
            "Content-Type": "application/json; charset=UTF-8"
        },
        data: {},
        // Custom events.
        onSuccessStart: function (response, status, xhr, self) {
        },
        onSuccessFinish: function (response, status, xhr, self, filename) {
        },
        onErrorOccured: function (response, status, xhr, self) {
        }
    };
    this.download = function () {
        var self = this;
        $.ajax({
            type: this.settings.type,
            url: this.settings.url,
            headers: this.settings.headers,
            data: this.settings.data,
            success: function (response, status, xhr) {
                // Start custom event.
                self.settings.onSuccessStart(response, status, xhr, self);

                // Check if a filename is existing on the response headers.
                var filename = "";
                var disposition = xhr.getResponseHeader("Content-Disposition");
                if (disposition && disposition.indexOf("attachment") !== -1) {
                    var filenameRegex = /filename[^;=\n]*=(([""]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1])
                        filename = matches[1].replace(/[""]/g, "");
                }

                var type = xhr.getResponseHeader("Content-Type");
                var blob = new Blob([response], {type: type});

                if (typeof window.navigator.msSaveBlob !== "undefined") {
                    // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed.
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);

                    if (filename) {
                        // Use HTML5 a[download] attribute to specify filename.
                        var a = document.createElement("a");
                        // Safari doesn"t support this yet.
                        if (typeof a.download === "undefined") {
                            window.location = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location = downloadUrl;
                    }

                    setTimeout(function () {
                        URL.revokeObjectURL(downloadUrl);
                    }, 100); // Cleanup
                }

                // Final custom event.
                self.settings.onSuccessFinish(response, status, xhr, self, filename);
            },
            error: function (response, status, xhr) {
                // Custom event to handle the error.
                self.settings.onErrorOccured(response, status, xhr, self);
            }
        });
    };
    // Constructor.
    {
        // Merge settings.
        $.extend(this.settings, configurationSettings);
        // Make the request.
        this.download();
    }
};

I created this class to added to my JS library. It is reusable. Hope that helps.

Lemming answered 17/1, 2015 at 17:32 Comment(6)
Blob object is supported in IE10+.Phytopathology
I had to set the responseType of xhr to arraybuffer or blob for this to work. (Otherwise, this works great.)Bellringer
I had the exact same question. All the people responding "just make it a link" doesn't help the OP. If your content is dynamic and the link you are going to is dynamic, you have to jquery it all... the answer for me was to put a form on the page with all hidden inputs (isn't shown to the user) and then fill it out and submit it with jquery. Works great.Vagary
This is a great answer, but for some reason, I just keep getting broken empty PDF. Can't figure it out. When I return same byteset through the API - it's fine, so it's something to do with MVC response. I use FileResult response type: File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);Bard
Clarification: if open the URL through address bar - file is correctly opened. If I use AJAX + blob to get the file - file is malformed.Bard
Update: I was able to fix it by using this answer: github.com/eligrey/FileSaver.js/issues/…Bard
C
9

What worked for me is the following code, as the server function is retrieving File(memoryStream.GetBuffer(), "application/pdf", "fileName.pdf");:

$http.get( fullUrl, { responseType: 'arraybuffer' })
            .success(function (response) {
                var blob = new Blob([response], { type: 'application/pdf' });

                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob); // for IE
                }
                else {
                    var fileURL = URL.createObjectURL(blob);
                    var newWin = window.open(fileURL);
                    newWin.focus();
                    newWin.reload();
                }
});
Confess answered 17/7, 2016 at 15:24 Comment(1)
This works for me perfectly at the time of this comment and newest ChromeDeathlike
C
6

You could use this plugin which creates a form, and submits it, then removes it from the page.

jQuery.download = function(url, data, method) {
    //url and data options required
    if (url && data) {
        //data can be string of parameters or array/object
        data = typeof data == 'string' ? data : jQuery.param(data);
        //split params into form inputs
        var inputs = '';
        jQuery.each(data.split('&'), function() {
            var pair = this.split('=');
            inputs += '<input type="hidden" name="' + pair[0] +
                '" value="' + pair[1] + '" />';
        });
        //send request
        jQuery('<form action="' + url +
                '" method="' + (method || 'post') + '">' + inputs + '</form>')
            .appendTo('body').submit().remove();
    };
};


$.download(
    '/export.php',
    'filename=mySpreadsheet&format=xls&content=' + spreadsheetData
);

This worked for me. Found this plugin here

Cleanup answered 17/7, 2012 at 9:57 Comment(1)
This plugin just creates a form, and submits it, then removes it from the page. (if anyone was wondering)Phytopathology
D
6

Concerning the answer given by Mayur Padshala this is the correct logic to download a pdf file via ajax but as others report in the comments this solution is indeed downloads a blank pdf.

The reason for this is explained in the accepted answer of this question: jQuery has some issues loading binary data using AJAX requests, as it does not yet implement some HTML5 XHR v2 capabilities, see this enhancement request and this discussion.

So using HTMLHTTPRequest the code should look like this:

var req = new XMLHttpRequest();
req.open("POST", "URL", true);
req.responseType = "blob";
req.onload = function (event) {
    var blob = req.response;
    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="name_for_the_file_to_save_with_extention";
    link.click();
}
Deify answered 1/5, 2018 at 10:8 Comment(0)
O
6

To fix the blank PDF issue in post request to get stream data like PDF, we need to add response type as 'arraybuffer' or 'blob' in request

$.ajax({
  url: '<URL>',
  type: "POST",
  dataType: 'arraybuffer',
  success: function(data) {
    let blob = new Blob([data], {type: 'arraybuffer'});
    let link = document.createElement('a');
    let objectURL = window.URL.createObjectURL(blob);
    link.href = objectURL;
    link.target = '_self';
    link.download = "fileName.pdf";
    (document.body || document.documentElement).appendChild(link);
    link.click();
    setTimeout(()=>{
        window.URL.revokeObjectURL(objectURL);
        link.remove();
    }, 100);
  }
});
Overfly answered 10/1, 2019 at 5:40 Comment(0)
H
5

The following code worked for me

//Parameter to be passed
var data = 'reportid=R3823&isSQL=1&filter=[]';
var xhr = new XMLHttpRequest();
xhr.open("POST", "Reporting.jsp"); //url.It can pdf file path
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.responseType = "blob";
xhr.onload = function () {
    if (this.status === 200) {
        var blob = new Blob([xhr.response]);
        const url = window.URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = url;
        a.download = 'myFile.pdf';
        a.click();
        setTimeout(function () {
            // For Firefox it is necessary to delay revoking the ObjectURL
            window.URL.revokeObjectURL(data)
                , 100
        })
    }
};
xhr.send(data);
Hali answered 28/11, 2018 at 11:34 Comment(0)
B
3

Hope this will save you a few hours and spare you from a headache. It took me a while to figure this out, but doing regular $.ajax() request ruined my PDF file, while requesting it through address bar worked perfectly. Solution was this:

Include download.js: https://www.npmjs.com/package/downloadjs

Then use XMLHttpRequest instead of $.ajax() request.

    var ajax = new XMLHttpRequest(); 
    
    ajax.open("GET", '/Admin/GetPdf' + id, true); 
    ajax.onreadystatechange = function(data) { 
        if (this.readyState == 4)
        {
            if (this.status == 200)
            {
                download(this.response, "report.pdf", "application/pdf");

            }
            else if (this.responseText != "")
            {
                alert(this.responseText);
            }
        }
        else if (this.readyState == 2)
        {
            if (this.status == 200)
            {
                this.responseType = "blob";
            }
            else
            {
                this.responseType = "text";
            }
        }
    };

    ajax.send(null);
Bard answered 5/2, 2019 at 10:20 Comment(2)
I spent several hours on the same problem. PDF font won't be loaded correctly. Thank you!Saum
Broken link already.Isidor
A
2

create a hidden iframe, then in your ajax code above:

url: document.getElementById('myiframeid').src = your_server_side_url,

and remove the window.open(response);

Avenue answered 14/9, 2011 at 12:57 Comment(2)
This solution worked like a charm. I'm calling a server side script which makes a curl call to a service which fetches the file via curl. This works great as I can drop a loading gif and disable the request link.Rhododendron
This solution works for GET requests not for POST requests as in the original post.Implode
M
2

This snippet is for angular js users which will face the same problem, Note that the response file is downloaded using a programmed click event. In this case , the headers were sent by server containing filename and content/type.

$http({
    method: 'POST', 
    url: 'DownloadAttachment_URL',
    data: { 'fileRef': 'filename.pdf' }, //I'm sending filename as a param
    headers: { 'Authorization': $localStorage.jwt === undefined ? jwt : $localStorage.jwt },
    responseType: 'arraybuffer',
}).success(function (data, status, headers, config) {
    headers = headers();
    var filename = headers['x-filename'];
    var contentType = headers['content-type'];
    var linkElement = document.createElement('a');
    try {
        var blob = new Blob([data], { type: contentType });
        var url = window.URL.createObjectURL(blob);

        linkElement.setAttribute('href', url);
        linkElement.setAttribute("download", filename);

        var clickEvent = new MouseEvent("click", {
            "view": window,
            "bubbles": true,
            "cancelable": false
        });
        linkElement.dispatchEvent(clickEvent);
    } catch (ex) {
        console.log(ex);
    }
}).error(function (data, status, headers, config) {
}).finally(function () {

});
Monitorial answered 9/4, 2018 at 4:20 Comment(1)
Please write some explanation for your answer.Spartan
E
2

I have found a solution that solved this problem for me (blank pdf when using jquery ajax). I've found this magical solution here: https://www.py4u.net/discuss/904599 (Answer 2) and it involves adding xhrFields to your ajax call:

xhrFields: {
   responseType: 'blob'
}

My working example:

$.ajax({
      url: "myUrl",
      type: 'GET',
      headers: {"token": mySecurityToken},
      xhrFields: {
                responseType: 'blob'
      },
      data: {id: myId}
    }).done(function( data, statusText, xhr ) {
        var filename = "";
        var disposition = xhr.getResponseHeader("Content-Disposition");
        if (disposition && (disposition.indexOf("attachment") !== -1) || disposition.indexOf("filename") !== -1) {
            var filenameRegex = /filename[^;=\n]*=(([""]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1])
                filename = matches[1].replace(/[""]/g, "");
        }

        var type = xhr.getResponseHeader("Content-Type");
        var blob = new Blob([data], {type: type});

        if (typeof window.navigator.msSaveBlob !== "undefined") {
            // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed.
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);

            if (filename) {
                // Use HTML5 a[download] attribute to specify filename.
                var a = document.createElement("a");
                // Safari doesn"t support this yet.
                if (typeof a.download === "undefined") {
                    window.location = downloadUrl;
                } else {
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                }
            } else {
                window.location = downloadUrl;
            }

            setTimeout(function () {
                URL.revokeObjectURL(downloadUrl);
            }, 100); // Cleanup
        }

    })

I hope this will solve this nasty issue for many of you.

Electrojet answered 23/12, 2021 at 21:37 Comment(0)
T
1

var xhr;
var beforeSend = function(){
    $('#pleasewaitDL').modal('show');
}
$(function () {
    $('#print_brochure_link').click(function(){
        beforeSend();
        xhr = new XMLHttpRequest();
        xhr.open("GET",$('#preparedPrintModalForm').attr('action'), true); 
        xhr.responseType = "blob";
        xhr.onload = function (e) {
            if (this.status === 200) {
                var file = window.URL.createObjectURL(this.response);
                var a = document.createElement("a");
                a.href = file;
                a.download = this.response.name || "Property Brochure";
                console.log(file);
                document.body.appendChild(a);
                a.click();
                
                window.onfocus = function () {                     
                  document.body.removeChild(a)
                }
                $('#pleasewaitDL').modal('hide');
            };
        };
        xhr.send($('#preparedPrintModalForm').serialize());
    });
    $('#pleasewaitDLCancel').click(function() {
        xhr.abort();
    });
});
Time answered 7/4, 2017 at 2:56 Comment(0)
H
1

If you have to work with file-stream (so no physically saved PDF) like we do and you want to download the PDF without page-reload, the following function works for us:

HTML

<div id="download-helper-hidden-container" style="display:none">
     <form id="download-helper-form" target="pdf-download-output" method="post">
            <input type="hidden" name="downloadHelperTransferData" id="downloadHelperTransferData" />
     </form>
     <iframe id="pdf-helper-output" name="pdf-download-output"></iframe>
</div>

Javascript

var form = document.getElementById('download-helper-form');
$("#downloadHelperTransferData").val(transferData);
form.action = "ServerSideFunctionWhichWritesPdfBytesToResponse";
form.submit();

Due to the target="pdf-download-output", the response is written into the iframe and therefore no page reload is executed, but the pdf-response-stream is output in the browser as a download.

Halt answered 20/10, 2017 at 6:4 Comment(1)
sorry, but how do you get transferData value?Funeral
C
0

Do you have to do it with Ajax? Couldn't it be a possibility to load it in an iframe?

Contraction answered 4/1, 2010 at 13:44 Comment(1)
I am checking if, this could be done with Ajax. If it is technically impossible or an inferior approach, i'd switch to other approaches.Soulier
C
0

100% OK for all file types

// download the file
var link = document.createElement('a'),
    filename = fname;
link.href = URL.createObjectURL(data);
link.download = filename;
link.click();
Chadburn answered 24/2, 2022 at 4:14 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Keratogenous
H
0

The best usage is to do an anchor or a form with the provided link, but it you need to do a validation or in other cases using jquery the best usage is to add a form and submit it using jquery (don't forget to set your request disposition as attachement on server side).

<form id="pdf-form" action="/link_to/download_your.pdf" accept-charset="UTF-8" method="get">
    <input type="hidden" name="data" id="data" value="your data"></form>

and

<a href="javascript:void(0);" id="pdf">Download my Pdf</a>

then in jquery

$('#pdf').click(function () {
// your data if it json do it like this JSON.stringify(your_data_as_json)
$('#data').val(data);
$('#pdf-form').submit();
})
Headwind answered 4/6, 2022 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.