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 http://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(“http://www.google.com”+e)
}