すべての添付ファイルをダウンロードする方法

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" 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
  return;
}
 
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 = "AllAttachments.zip";
} else if (!zipFileName.toLowerCase().endsWith(".zip")) {
  zipFileName = zipFileName + ".zip";
}
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentType("application/zip");
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++) {
  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];
    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();
  }
}
 
downloadDocument.recycle();
zipOutStream.flush();
zipOutStream.close();
outStream.flush();
outStream.close();
facesContext.responseComplete();}]]>
  </xp:this.beforeRenderResponse>
</xp:view>





上のコードでXPageを作成します。例えば、xDownloadAllAttachments.xsp としましょう。次にこのXPageをコールしますが、クエリーストリングに文書のUNIDを渡します。
例:http://<SERVER>/<DATABASE PATH>/ xDownloadAllAttachments.xsp? documentUNID=<DOCUMENT UNID>&zipFileName=<NAME OF ZP FILE>

XPages
katoman
August 19, 2015 2:56 PM
Rating
0





No comments yetLogin first to comment...