Want to open PDF file from DWR request

2019-08-03 01:40发布

I want to open a pdf file from DWR call. I am using DWR 2.0 Please guide me.

标签: java dwr
2条回答
霸刀☆藐视天下
2楼-- · 2019-08-03 02:08

I am not able to understand that what do you mean by open pdf file. AS per my understanding of your question, you can use DWR to get the pdf file from server to client side. Then you have to display it with any client side techniques.

In DWR website you can find dwr.war file. It contains a example for downloading pdf files. You have put that file in any servlet container's web-apps folder. Then you can access the tutorial through http://localhost:8080/dwr/.

查看更多
Deceive 欺骗
3楼-- · 2019-08-03 02:33

Consider upgrading to DWR 3.

In DWR3 you will find a FileTransfer object...

HTML:

<form action="downloadFile.do">
  <div id="buttons">
    <input type="button" value="Back" id="mapback"    onClick="javascript:back();" class="Back" />
    <input type="button" value="SavePDF" id="SavePDF" onclick="return downloadPdfFile();" class="saveToPdfButton" />
    <input type="submit" value="Continue" id="continue" class="continue" />

  </div>
</form>

JavaScript:

function downloadPdfFile() {
  dwr.engine.setTimeout(59000);

  var callMetaData = {
  callback : downloadPdfCallback,
  exceptionHandler : hideLoadingAndSwitchOnSavePDFButton,
  errorHandler : hideLoadingAndSwitchOnSavePDFButton,
  preHook : showLoading,
  postHook : hideLoading
};

DWRAction.downloadPdfFile(callMetaData);

return false;
}

function downloadPdfCallback(data) {
   dwr.engine.openInDownload(data);
}

Java:

File pdfFile = new File(pdfFilename);
try {
      InputStream is = new BufferedInputStream(new FileInputStream(pdfFile));
       dwrPdfFile = new FileTransfer(pdfFile.getName(), "application/pdf", is);
     } catch (FileNotFoundException e) {
       LOGGER.error("Unable to find PDF file \'{}\'", pdfFile.getAbsoluteFile());
     } 
查看更多
登录 后发表回答