As I was reviewing my pilot project with a satisfied eye, my team lead came up with the suggestion that we needed to do something to make it even more attractive to the customer. As he was clearly thinking about functionality that could be given, I suggested that we allow the attachments that came with the document to be zipped as the customer downloaded the data from the Web. I have given below a sample database which accomplishes this task.
In this database, I have uploaded attachments – 3 photographs (as attachments) and an Excel sheet, and then downloaded the same using XPages – DownloadFiles. This will give the attachments in a zipped folder.
and using the DownloadFiles Xpage, I downloaded, the attachments which got automatically zipped into a file with the CompanyFiles.zip name.
This code was adapted from his OpenNTF submission – from https://openntf.org/XSnippets.nsf/snippet.xsp?id=download-all-attachments.
This has been achieved with the following code:
try
{
//get the name of the person
var Name =sessionScope.Company
//get the ViewName
var viewAttach:NotesView=database.getView(“Candidates”)
//get the particular document handle based on the selected candidate
var dc:NotesDocumentCollection=viewAttach.getAllDocumentsByKey(Name)
var downloadDocument:NotesDocument=dc.getFirstDocument()
if(downloadDocument!=null)
{
var attachments:java.util.Vector = session.evaluate(“@AttachmentNames”, downloadDocument);
// If there are no attachments then STOP the execution of the code!
if (attachments == null || (attachments.size() == 1 && attachments.get(0).toString().trim().equals(“”)))
this.setRendered(true); // Show the XPage
return;
}
var externalContext:javax.faces.context.ExternalContext = facesContext.getExternalContext()
var response:javax.servlet.http.HttpServletResponse = externalContext.getResponse();
//download the attachments as person’sFiles.zip
Name=@ReplaceSubstring(@Trim(Name),” “,”_”)
zipFileName = Name+”Files.zip”;
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, -1);
response.setContentType(“application/zip”);
//creating Zip Application Files
response.setHeader(“Content-Disposition”, “attachment; filename=” + zipFileName);
var outStream:java.io.OutputStream = response.getOutputStream();
var zipOutStream:java.util.zip.ZipOutputStream = new java.util.zip.ZipOutputStream(outStream);
var embeddedObj:NotesEmbeddedObject = null;
var bufferInStream:java.io.BufferedInputStream = null;
// Loop through all the attachments
for (var i = 0; i < attachments.size(); i++) {
//get the embedded Object from the document using get Attachment method
embeddedObj = downloadDocument.getAttachment(attachments.get(i).toString());
if (embeddedObj != null) {
bufferInStream = new java.io.BufferedInputStream(embeddedObj.getInputStream());
var bufferLength = bufferInStream.available();
var data = new byte[bufferLength];
//Using Buffer , zipping the contents
bufferInStream.read(data, 0, bufferLength);
// Read the attachment data
var entry:java.util.zip.ZipEntry = new java.util.zip.ZipEntry(embeddedObj.getName());
zipOutStream.putNextEntry(entry);
zipOutStream.write(data); // Write attachment into Zip
bufferInStream.close();
embeddedObj.recycle();
}
}
// refresh and close the objects
downloadDocument.recycle();
zipOutStream.flush();
zipOutStream.close();
outStream.flush();
outStream.close();
//response has been completed
facesContext.responseComplete();
}
else
{
this.setRendered(true);
}
}
catch(e)
{
facesContext.getExternalContext().redirect(“https://www.google.com”+e)
}
Great solution, but I have a problem: after downloanding the file, the originator xPage freezes, and if I try any other button on the page a get a error message on the server console: HTTP JVM: com.ibm.xsp.webapp.FacesServlet$ExtendedServletException: java.lang.IllegalStateException: Can’t get a Writer while an OutputStream is already in use
Do y have any idea on how to fix this ?
Hi Richard,
Thanks for trying it out. You are correct. We too faced the same problem and are trying to check out various methods to get over this. Will definitely post the solution as soon as I get it.
Hi, Vijay
Thanks for the response, I hope you get it solved, it’s a great solution !
The code seems to be taken from OpenNTF XSnippet (http://openntf.org/XSnippets.nsf/snippet.xsp?id=download-all-attachments). Even the comment “// Show the XPage” has not been changed and the author writes “To this end, I wrote the code…”
The original code itself is under Apache License which allows you to modify and use it in any way you want (even in proprietary applications), it does require you to credit the original author and source (openntf.org).
Hi Naveen,
This is my first blog post and I was unaware of my mistake in not acknowledging the source. Please accept my apologies. I have modified my post to reflect the same and given a block update about the source.And thankyou for the great code, it did help me to solve the problem of my customer.