使用JavaScript在新窗口中打开PDF流窗口中、JavaScript、PDF

2023-09-10 22:08:19 作者:ヤ僐變莮嚼╮

我使用 Struts2的框架道场的UI。我想显示在新的浏览器窗口中的PDF格式。我通过正常的Ajax调用get方法获取PDF格式的InputStream从服务器(没有使用道场AJAX调用。使用本机JS ajax调用)。 如何在新的浏览器窗口中查看PDF格式字符串?

JS code:

/ 两个输入来获得PDF文件 /

 变种selectedFileSlot={"END407":"report_28_03_13.pdf","END408":"[B@cef121c","END409":"*.pdf;*.doc;*.docx;*.odt;*.ods","END410":"5242880"}
        VAR的SelectedNode = {对象ID:22df1b2601a3552f24e5f011abc27f86,规范ID:2001,entityConcreteID:11000}


    VAR XMLHTTP;
    如果(window.XMLHtt prequest){
        XMLHTTP =新XMLHtt prequest();
    }
    xmlhttp.onreadystatechange =功能(){
      如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
          警报(xmlhttp.response);
          的window.open(xmlhttp.response); //不能够在新的浏览器窗口中打开
      }
    }
    xmlhttp.open(GET,downloadDocument.action的SelectedNode =?+ JSON.stringify(的SelectedNode)+&放大器; selectedFileSlot =+ JSON.stringify(selectedFileSlot),TRUE);
    xmlhttp.send();
 

的struts.xml code:

 <作用NAME =downloadDocument级=commonAction方法=downloadDocument>
    <拦截-REF名=sessionStack>< /拦截-REF>
    <拦截-REF名=cachingStack>< /拦截-REF>
    <结果名称=成功TYPE =流>
    < PARAM NAME =的contentType>应用程序/八位字节流< /参数>
    < PARAM NAME =inputName>的InputStream< /参数>
    < PARAM NAME =contentDisposition>附件;文件名=$ {fileFileName}< /参数>
    < PARAM NAME =BUFFERSIZE> 1024< /参数>
    < /结果>
    < /作用>
 

动作类code:

  commonDTO.setSelectedNode(this.selectedNode);
commonDTO.setSelectedFileSlot(this.selectedFileSlot);
字节[]字节= commonDAO.getDownloadDocument(co​​mmonDTO); //获取PDF格式的字节数组
的InputStream =新ByteArrayInputStream的(字节);
 

解决方案

这是PDF文件的二进制内容,你不能在Javascript处理,并且需要用应用程序/ PDF 内容类型返回(不的text / html 应用程序/八位字节流),用的流结果。

为什么地球上,你使用AJAX来加载它的在新窗口中?只需使用以下方式之一。

I am using struts2 framework and dojo for ui. I want to display a pdf in new browser window. I am getting pdf inputstream from server via normal ajax call get method(didnt use dojo ajax call. used native js ajax call ). How to view pdf string in a new browser window?

js code:

/two inputs to get 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);//not able 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); //getting pdf as byte array
inputStream = new ByteArrayInputStream(bytes);

解决方案

That is the binary content of the PDF file, that you can't handle in Javascript, and that you need to return with application/pdf Content-type (not text/html or application/octet-stream), with a Stream result.

Why on earth are you using AJAX to load it in a new window ? Just use one of the following way.