javascript - Download PDF from Byte array using ajax - Stack Overflow

admin2025-04-22  0

I am trying below code but this is creating file but not showing content. I need your anyone help, What i am doing wrong.

    $.ajax({
    type: "POST",
    async : false,
    url: "/searchModel/createPDF",
    data:"my_param",
    contentType: 'application/octet-stream',  
    beforeSend:function(){

    }, 
    success: function(html) {
        /* html value is [37,80,68,75 .........] */
        //var file = new Blob([html], {type: 'application/pdf'});
        //var fileURL = URL.createObjectURL(file);
        //window.open(fileURL);

        var blob=new Blob([html],{type: 'application/pdf'});
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download="SearchedResults.pdf";
        link.click();

    }
 }); 

Response ing from server is in byte array [37,80,68,75 .........]

Please help me if data in bytes array how it would be convert in pdf.

I am trying below code but this is creating file but not showing content. I need your anyone help, What i am doing wrong.

    $.ajax({
    type: "POST",
    async : false,
    url: "/searchModel/createPDF",
    data:"my_param",
    contentType: 'application/octet-stream',  
    beforeSend:function(){

    }, 
    success: function(html) {
        /* html value is [37,80,68,75 .........] */
        //var file = new Blob([html], {type: 'application/pdf'});
        //var fileURL = URL.createObjectURL(file);
        //window.open(fileURL);

        var blob=new Blob([html],{type: 'application/pdf'});
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download="SearchedResults.pdf";
        link.click();

    }
 }); 

Response ing from server is in byte array [37,80,68,75 .........]

Please help me if data in bytes array how it would be convert in pdf.

Share Improve this question edited Oct 17, 2015 at 4:13 Atul Jain asked Oct 16, 2015 at 16:04 Atul JainAtul Jain 1,0852 gold badges16 silver badges24 bronze badges 6
  • FYI: contentType: 'application/octet-stream' is for the Content-type of the request payload. – gen_Eric Commented Oct 16, 2015 at 16:11
  • you don't need ajax here .. just specify the url your anchor link .. you cannot download file through Ajax.. – Kishore Sahasranaman Commented Oct 16, 2015 at 16:27
  • Is byte data is correct to show create pdf.If i change content-type then it would be work right. – Atul Jain Commented Oct 16, 2015 at 17:18
  • This showing byte array in browser window – Atul Jain Commented Oct 17, 2015 at 4:18
  • Okay no problems i have to convert byte array to render as pdf file then its fine and working – Atul Jain Commented Oct 17, 2015 at 4:22
 |  Show 1 more ment

2 Answers 2

Reset to default 1

You cannot use AJAX to download files. It doesn't make sense. You can send the AJAX request and fetch the file contents inside the success handler on the client, but for obvious security reasons you can't do much with it. You cannot save it on the client puter and there's no javascript API allowing you to prompt the user where to save it.

So to download files, don't use AJAX. Create an anchor pointing to your server side script that serves the file to be downloaded.

Sample :

window.downloadfile = function(e){
  window.location = "/searchModel/createPDF?" +  "my_param";
}
<a href="#" onclick="downloadfile()">download file</a>

Yes you people said correct does not mean while download PDF using AJAX.

 window.location = "/searchModel/createPDF?" + "my_param";

is enough,Only we need to render pdf from server side. It is default downloadable.Thanks for your suggestions

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745284368a294300.html

最新回复(0)