Previous code PF 3.4
Code: Select all
<p:commandLink id="file_download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)">
<p:fileDownload value="#{fileDownloadController.downloadFile}"/>
</p:commandLink>
@ManagedBean(name = "fileDownloadController")
@ViewScoped
public class FileDownloadController implements Serializable{
public StreamedContent getDownloadFile() {
downloadFile = newDownloadFile();
return downloadFile;
}
private StreamedContent newDownloadFile() {
InputStream downloadStream = new ByteArrayInputStream(... fetching a BLOB from Oracle db as byte[] ...);
// contentType = "application/pdf", fileName = "somefile.pdf"
// The file is not a physical file it's only as IntputStream
downloadF = new DefaultStreamedContent(downloadStream, contentType, fileName);
try {
downloadStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return downloadF;
}
}
Trying to use Spring Boot that's why the Controller annotations are from that framework.
All other PF functionality works ok with Spring Boot.
The page controller scope is @Scope(value = ViewScope.SCOPE_VIEW), where ViewScope is a custom class which brings javax.faces.context.FacesContext.getCurrentInstance().getViewRoot().getViewMap() into the application.
Code: Select all
<p:commandLink id="file_download">
<p:fileDownload value="#{fileDownloadController.getDownloadFile(controller.note)}"/>
</p:commandLink>
@Scope(value= WebApplicationContext.SCOPE_REQUEST)
@Component(value = "fileDownloadController") // Name of the bean in Spring IoC
@ELBeanName(value = "fileDownloadController") // Name of the bean used by JSF
public class FileDownloadController {
public StreamedContent getDownloadFile(Note note) {
downloadFile = newDownloadFile(note);
return downloadFile;
}
private StreamedContent newDownloadFile(Note note) {
StreamedContent downloadF = null;
// contentType = "application/pdf", fileName = "somefile.pdf"
byte[] attachment = ... fetching a BLOB from Oracle db as byte[] ...;
downloadF = DefaultStreamedContent.builder()
.name(fileName)
.contentType(contentType)
.stream(() -> new ByteArrayInputStream(attachment))
.build();
return downloadF;
}
}
The problem
The resulting downloadF seems to be corrupt.
Firefox does not recognize its type and assumes it's a json file when trying to open it directly.
When saved to the disk it cannot be opened due to "it is either an not supportted file type or because the file has been damaged" .
Other browsers react in a similar manner.
I tried to play with the DefaultStreamedContent.setContentEncoding but there is no clear direction on that, and I got nowhere.
Any help would be greatly appreciated.