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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745284367a4620472.html

相关推荐

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

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

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信