Currently, I am utilizing the struts2
framework alongside dojo
for the UI. My goal is to display a PDF in a new browser window. The PDF inputstream is obtained from the server through a standard AJAX call using the GET method (not utilizing a Dojo AJAX call but instead opting for a native JS AJAX call). How can I go about displaying the PDF string in a new browser window?
JS Code:
// Two inputs to retrieve the PDF file //
var selectedFileSlot={"END407":"report_28_03_13.pdf","END408":"[B@cef121c","END409":"*.pdf;*.doc;*.docx;*.odt;*.ods","END410":"5242880"}
var selectedNode= {"objectID":"22df1b2601a3552f24e5f011abc27f86","specID":"2001","entityConcreteID":"11000"}
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.response);
window.open(xmlhttp.response);//Unable to open in new browser window
}
}
xmlhttp.open("GET","downloadDocument.action?selectedNode=" + JSON.stringify(selectedNode) + "&selectedFileSlot=" + JSON.stringify(selectedFileSlot),true);
xmlhttp.send();
struts.xml Code:
<action name="downloadDocument" class="commonAction" method="downloadDocument">
<interceptor-ref name="sessionStack"></interceptor-ref>
<interceptor-ref name="cachingStack"></interceptor-ref>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileFileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
Action Class Code:
commonDTO.setSelectedNode(this.selectedNode);
commonDTO.setSelectedFileSlot(this.selectedFileSlot);
byte[] bytes = commonDAO.getDownloadDocument(commonDTO); //Obtaining PDF as a byte array
inputStream = new ByteArrayInputStream(bytes);