1

I am sending a ajax request on click of LINK from there i am getting a URL of PDF file now after getting the response i want to download that particular file on user's system.But i am not able to download that particular file on user system.It is getting opened on the browser url, i don't want to show the url of PDF file to the user.How can we do this in jquery.

HTML

<li><a href="javascript:void(0)" data-unid ="1" class ="cert_download">Download</a></li>

Jquery

$.ajax({
    type:"POST",
    url: myurl,
    dataType: "json",
    data:data,
    error: function(XMLHttpRequest, textStatus, errorThrown){
        alert("There is some problem in our system please try after sometimes");
    },
    success: function(data){
        if(data.url){
            if (!window.location.origin){
                window.location.origin = window.location.protocol+"//"+window.location.host+"/";
                location.href = window.location.origin+data.url;
            }
        }
    }
});

I don't want to show user the URL and even i don't want to download the file in a new tab/window.As he click on link i want to download the file to his sytem How can we download the file to the user.Please help me in this

Developer
  • 6,292
  • 19
  • 55
  • 115

2 Answers2

1

This will work for u :

function downloadURI(uri, name){
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    $("#data_cert").html(link);
    link.click();
    $("#data_cert").html("");
}
Pooja Dubey
  • 683
  • 2
  • 14
  • 34
-1

Just open new window with

window.open('link to pdf', '_blank')

And send the pdf with

header('Content-Disposition: attachment; filename=some.pdf')

Window will close as soon someone click download.

tkeram
  • 214
  • 1
  • 5