Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    epb
    epb is offline
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Output byte array as PDF from javascript

    Hi all

    I have the following ajax call from a webpage. The url points to a webservice (C#) which returns a PDF file as a byte array. How do I output the byte array (in the variable 'result') as a PDF for the user to save using javascript?

    Code:
    $.ajax({
                    type: "GET",
                    async: "false",
                    url: url,
                    data: "",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    processData: true,
                    cache: false,
                    success: function (result) {
                        // TODO: What to do here??
                    },
                    error: function(xmlHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown.message);
                    }
                });

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    you need to turn your array into a string, which i can't advise on without seeing an example server response.

    once you have a binary string, it's easy enough to download in newer browsers:

    Code:
    function download(strData, strFileName, strMimeType){
      var  D=document, A=arguments, a=D.createElement("a"), 
           d=A[0], n=A[1], t=A[2]||"text/plain";
    
        //build download link:
           a.href="data:" +strMimeType+ "," +escape(strData);
    
    if('download' in a){
         a.setAttribute("download", n);
         a.innerHTML="downloading...";
         D.body.appendChild(a);
        setTimeout(function(){
    	var e= D.createEvent("MouseEvents");
    	e.initMouseEvent(
    		"click", true, false, window, 0, 0, 0, 0, 0
    		, false, false, false, false, 0, null
    	);
           a.dispatchEvent(e);
           D.body.removeChild(a);
        }, 66 );
      return true;
    };//end if a[download]?
    
     //do iframe dataURL download:
        var f=D.createElement("iframe");
           D.body.appendChild(f);
            f.src="data:" +(A[2]?A[2]:"application/octet-stream")+ (window.btoa?";base64":"") +","  +(window.btoa?window.btoa:escape)(strData);
            setTimeout(function(){D.body.removeChild(f);}, 333);
        return true;
    }//end download()
    
    
    //example usage:
    download("hello world", "test.txt", "text/plain")

    you syntax would be something like
    Code:
    download( result.chars.join(""), report.pdf", "application/pdf");

    at this point, only chrome will respect the filename, with firefox adding support in feb or april. It still will download correctly without the file-name, but you have to rename it on the OS, something which is not always user-friendly.


    see https://github.com/dcneiner/Downloadify for an IE-friendly version that uses flash.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •