How to use dojo or esriRequest object to get pdf that is stream in a byte array

618
1
Jump to solution
09-19-2017 01:13 PM
BrettLawson
New Contributor II

I have an SOE that creates a pdf and streams it back in the response and it works great.  Now how can I call it using dojo or the esriRequest object to call the SOE and display the resulting pdf in a new window?  I see the esriRequest has a handleAs that can be set to document, but I fail to see how it is implemented.  I have seen examples of using dojo.io.iframe, but I prefer not to use a deprecated library.

var downloadPdfIframeName = "downloadPdfIframe";
var iframe = dojo.io.iframe.create(downloadPdfIframeName);
dojo.io.iframe.setSrc(iframe, url, true);

I would be nice if I could just use straight javascript and do something like window.open(encodeURIComponent(url)), but the url would be too long after I add all the parameters to the querystring and it would have to be a post request.

Any help piecing it together would be appreciated.

Thanks

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
BrettLawson
New Contributor II

In case anybody else comes across a similar problem, this is what I ended up doing:

var requestHandle = esriRequest({
url: baseURL,
content: content,
handleAs: 'arraybuffer',
callbackParamName: 'callback'
});
requestHandle.then(
function (response) {
var file = new Blob([response], {
type: 'application/pdf'
});
//IE work around
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, 'ConsumptionReport.pdf');
}
else {
var fileURL = URL.createObjectURL(file);
//window.open(fileURL);
// create an anchor and click on it.
var ancorTag = document.createElement('a');
ancorTag.href = fileURL; ancorTag.target = '_blank';
ancorTag.download = 'ConsumptionReport.pdf';
document.body.appendChild(ancorTag);
ancorTag.click();
document.body.removeChild(ancorTag);
}
},
function (error) {
var s = error;
}

View solution in original post

1 Reply
BrettLawson
New Contributor II

In case anybody else comes across a similar problem, this is what I ended up doing:

var requestHandle = esriRequest({
url: baseURL,
content: content,
handleAs: 'arraybuffer',
callbackParamName: 'callback'
});
requestHandle.then(
function (response) {
var file = new Blob([response], {
type: 'application/pdf'
});
//IE work around
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, 'ConsumptionReport.pdf');
}
else {
var fileURL = URL.createObjectURL(file);
//window.open(fileURL);
// create an anchor and click on it.
var ancorTag = document.createElement('a');
ancorTag.href = fileURL; ancorTag.target = '_blank';
ancorTag.download = 'ConsumptionReport.pdf';
document.body.appendChild(ancorTag);
ancorTag.click();
document.body.removeChild(ancorTag);
}
},
function (error) {
var s = error;
}

View solution in original post