Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 02:03 PM   PM User | #1
epb
New to the CF scene

 
Join Date: Jan 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
epb is an unknown quantity at this point
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);
                }
            });
epb is offline   Reply With Quote
Old 01-11-2013, 05:38 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:57 PM.


Advertisement
Log in to turn off these ads.