-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
We are in the process of upgrading our application from zk5 to zk7. In zk7, filedownload.save is not working in chrome, ie etc.
Tried to use Filedownload.saveresumable as mentioned in the article but no luck.
Please advise.
Hello hmannam,
First of all for information regarding file download please refer to the following url:
http://books.zkoss.org/wiki/ZKComponentReference/Essential_Components/Filedownload
Furthermore in my environment using zk 7 it works as expected, but in order to help the best way is to post the following:
Best Regards,
Darksu
Hi Hmnnam,
I'am also using ZK7 and the FileDownload.save is running just fine. maybe you could post your code snippet and error log.
Warm Regards,
KleponBoy
Thanks for your replies and here is what you have requested for,
Zk version: 7.0.3 Chrome version: Version 45.0.2454.101 m (64-bit)
Sample code:
this is the code from controller class, if (feeds != null) { //TEMR-189 //if(feeds.size() != 0){ Span sp = new Span(); final Image img = new Image(); img.setSrc(IconUtil.getIconPath("download-icon.png", null, "temr")); img.setStyle("cursor:pointer; padding-left:180px"); img.setParent(sp); img.setTooltiptext("download previous feedback and student reflection"); previousFeedbackVlayout.appendChild(sp);
img.addEventListener(Events.ON_CLICK, new EventListener() {
@Override
public void onEvent(final Event event) throws Exception {
String studentName = service.getUser(((Person) (studentListbox.getSelectedItem().getValue())).getPersonId()).getUsername();
String practiceName = ((Practice) (practiceListbox.getSelectedItem().getValue())).getPracticeName();
String lessonName = ((Lesson) (lessonListbox.getSelectedItem().getValue())).getLessonName();
List<StudentReflection> reflections = studentService.getStudentReflection(s.getPersonId(), l.getLessonId());
TEMRUtil.pdfExport(studentName, practiceName, lessonName, currentUser.getUsername(), feeds, reflections);
}
});
//}
and this is the pdfExport method,
public static void pdfExport(String studentName, String practiceName, String lessonName, String currentUserName, List<studentreflectionfeedback> feeds, List<studentreflection> reflections ) throws IOException {
//name of the file to be downloaded
String fileName = "studentReflectionNoteSummary" + "_" + studentName+ "_" +
df.format(new Date())+ ".pdf" ;
//creates a temporary file in memory
File temp = File.createTempFile("SRNFeedbackSummary", ".pdf");
Document document = new Document();
FileInputStream fileInputStream = null;
try {
PdfWriter.getInstance(document, new FileOutputStream( temp));
document.open();
//actual content of the file is constructed here
document = addContent(document, studentName, practiceName, lessonName, currentUserName, feeds, reflections );
document.close();
//load the file
//File file = new File(temp.getAbsolutePath());
fileInputStream = new FileInputStream(temp.getAbsolutePath());
temp.deleteOnExit();
//zk filedownload
Filedownload.save(fileInputStream, "application/pdf", fileName);
} catch (Exception e) {
log.error("Error generating pdf ", e);
}/*finally{
fileInputStream.close();
}*/
}
I don't see any errors either in the logs or on the ui. I just see some flickering effect on ui with vertical scroll bar added to the page. (looks like as if it is downloading but actual download is not happening)
Also, this is the html source code i have noticed on chrome debug mode,
not sure why zk7 version is appending two more extra attributes (zxsrc and zxurl) in the html code. I am guessing that would be the problem. and can you pls tell me the purpose of those two attibutes?
zk7 version:
<iframe src="/tEMR/zkau/view/z_ifo/dwnmed-0/058/studentReflectionNoteSummary_STUDENT1_12-Oct-2015%2007:10.pdf" id="zk_download" name="zk_download" style="visibility:hidden;width:0;height:0;border:0" frameborder="0" z_xsrc="http://localhost:8080/tEMR/zkau/view/z_ifo/dwnmed-0/058/studentReflectionNoteSummary_STUDENT1_12-Oct-2015%2007:10.pdf" z_xurl="/tEMR/zkau/web/org/regenstrief/framework/ui/security/temr-login.zul"></iframe>zk5 version:
<iframe src="/tEMR/zkau/view/z_fb5/dwnmed-0/egn1/studentReflectionNoteSummary_STUDENT1_12-Oct-2015%2007:10.pdf" id="zk_download" name="zk_download" style="visibility:hidden;width:0;height:0;border:0" frameborder="0"></iframe>Hello hmannam,
Let's make it a little more simple:
try {
File file = new File(fileName);
Filedownload.save(file, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Please try this out, and let me know.
Best Regards,
Darksu
Hi Darksu,
Tried below code and no luck. Noticed the same behavior,
try{
File file = new File(temp.getAbsolutePath());
Filedownload.save(file, null);
} catch (Exception e) {
log.error("Error generating pdf ", e);
}
Side note, not sure if it's related because you're using a temp directory. But it's silly in my opinion just to make a temp file so that you can provide it to the end user (and then delete on exit.) Instead, we just have our service layer stream the file to a ByteArrayOutputStream and use toByteArray on it in the FileDownload.save. something like this for your stuff:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourPdfCreatorService.streamFile(paramsYouNeed, baos);
Filedownload.save(baos.toByteArray(), "application/pdf", "pdfName.pdf");
That being said, I also use ZK7 and Chrome without issues streaming back known files on the file system. Test it with a different known file - not one that you're trying to create in your system's temp dir. You also didn't post what Exception is being caught in your log.error section.
Asked: 2015-10-09 14:48:04 +0800
Seen: 55 times
Last updated: Oct 13 '15