I want to open a pdf file from DWR call. I am using DWR 2.0 Please guide me.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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/
.
回答2:
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());
}