Download all attachments

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="" rendered="false">
	<xp:span style="color:rgb(192,0,0)">Sorry, no attachments found in this document!</xp:span>
	<xp:this.beforeRenderResponse><![CDATA[#{javascript:var downloadDocument:NotesDocument = database.getDocumentByUNID(context.getUrl().getParameter("documentUNID"));
var attachments:java.util.Vector = session.evaluate("@AttachmentNames", downloadDocument);
// If there are no attachments then STOP!
if (attachments == null || (attachments.size() == 1 && attachments.get(0).toString().trim().equals(""))) {
	this.setRendered(true); // Show the XPage

var externalContext:javax.faces.context.ExternalContext = facesContext.getExternalContext();
var response:javax.servlet.http.HttpServletResponse = externalContext.getResponse();

// Get the name of the zip file to be shown in download dialog box
var zipFileName = context.getUrl().getParameter("zipFileName");
if (zipFileName == null || zipFileName.equals("")) {
	zipFileName = "";
} else if (!zipFileName.toLowerCase().endsWith(".zip")) {
	zipFileName = zipFileName + ".zip";
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);

var = response.getOutputStream();
var = new;
var embeddedObj:NotesEmbeddedObject = null;
var = null;

// Loop through all the attachments
for (var i = 0; i < attachments.size(); i++) {
	embeddedObj = downloadDocument.getAttachment(attachments.get(i).toString());
	if (embeddedObj != null) {
		bufferInStream = new;
		var bufferLength = bufferInStream.available();
		var data = new byte[bufferLength];, 0, bufferLength); // Read the attachment data
		var = new;
		zipOutStream.write(data); // Write attachment into Zip


I always loved the feature of “Download all attachments” in Gmail. So I decided we should have this option available in XPages also. Create an XPage with the code above and save it as say, "xDownloadAllAttachments.xsp". Now just call the XPage xDownloadAllAttachments by passing the document UNID in query string to it.

For e.g. http://<SERVER>/<DATABASE PATH>/ xDownloadAllAttachments.xsp? documentUNID=<DOCUMENT UNID>&zipFileName=<NAME OF ZP FILE>

That’s it, you are done!

Parameters to query string

1. documentUNID – Universal ID of the document in which attachments are present. This is a mandatory parameter.
2. zipFileName – Name of zip file when the download box is shown to the user. This is optional and if this parameter is not specified then by default name is taken.

Java version of the above code is available at:

Naveen Maurya
March 13, 2012 1:22 AM

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.

3 comment(s)Login first to comment...
jeniffer homes
(at 03:03 on 22.07.2016)
I always loved the feature of “Download all attachments” in Gmail.
Grégory DEVISE
(at 04:10 on 23.07.2013)
Really usefull snippet, thank you !

@Ferhat BULUT : You could change content type header replacing "response.setContentType("application/zip");" with "response.setContentType("gzip");"
Ferhat BULUT
(at 17:30 on 16.06.2012)
If you selected "GZip" in "Application Properties -> XPages tab -> HTML Generation -> Compression" property, system zips your file twice. You can download zipped file which is also GZipped (without file extension). It is confused me but I found the reason after a while :)
If you select "None" in related (writed above) property, you can download zip file with actual (attached in the document) file.
Anyway Thanks Naveen Maurya, It is good XSnippet.