当我尝试下载文件和bean抛出异常页面重新加载并从url中删除参数时,def prepare_dimensions(dimensions, dim_list):
for dimension in dimensions:
dimension['info'] = next(dim for dim in dim_list if dimension['id'] == dim['_id'])
出现问题:
<p:fileDownload>
bean代码:
<p:commandLink update=":gform:growl" ajax="false"
onclick="PrimeFaces.monitorDownload(null, stop)">
<i class="fa fa-paperclip fa-2x sc-badge-icon sc-border-right hover"></i>
<p:fileDownload value="#{taskFileController.getFile(cc.attrs.scFile)}"/>
</p:commandLink>
P.S。没有异常页面时不重新加载。
P.s.s. bean是public void getFile(ScFile scFile) throws IOException {
try {
File file = ftpController.downLoadFile(scFile);
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setHeader("Content-Disposition", "attachment;filename=\"" + scFile.getFileName() + "\"");
response.setContentLength((int) file.length());
input = new FileInputStream(file);
byte[] buffer = new byte[1024];
out = response.getOutputStream();
while (input.read(buffer) != -1) {
out.write(buffer);
out.flush();
}
FacesContext.getCurrentInstance().getResponseComplete();
} catch (GrowlException e) {
GrowlException.show(e);
} catch (Throwable err) {
err.printStackTrace();
GrowlException.show("Возникла ошибка при скачивании файла");
}
}
和@Named
答案 0 :(得分:2)
如Primefaces showcases所述,您的getFile()
方法必须返回StreamedContent
而不是写入响应输出,返回值可能不是null
:
public StreamedContent getFile(ScFile scFile) throws IOException {
File file = ftpController.downLoadFile(scFile);
InputStream stream = new FileInputStream(file);
String filename = scFile.getFileName();
String contentType = "attachment;filename=\"" + scFile.getFileName() + "\"";
StreamedContent file = new DefaultStreamedContent(stream, contentType, filename);
return file;
}
由于任何原因而阻止它成为null
可以以良好或肮脏的方式解决:
new ByteArrayInputStream(new byte[] {});
答案 1 :(得分:0)
我遇到了同样的问题,如果要下载的文件是 null
,我不想刷新页面。我通过在实际下载之前生成文件并检查他的存在来禁用让用户下载文件的按钮解决了这个问题:
<p:commandButton disabled="#{ReportBean.contentToDownload eq null}">
<p:fileDownload value="#{ReportBean.downloadPdf()}"/>
</p:commandButton>
我发现这个解决方案比让用户点击两次更优雅。用户讨厌点击两次。