Search code examples
xpageslotus-dominojava-iossjs

Xpages SSJS - Outputting files is slow for large files


I am trying to output pdf files i have on the local filesystem of the domino server using instructions from Steve Wissel's page(s). http://www.wissel.net/blog/d6plinks/shwl-7mgfbn

The file will get downloaded, but it takes a few minutes for files that are in the 20MB range. Is there a way to speed up the streaming?

    <?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">

    <xp:this.beforeRenderResponse><![CDATA[#{javascript://importPackage(java.io.File);
var exCon = facesContext.getExternalContext();
var response = exCon.getResponse();
var out = response.getOutputStream();

if (out==null) {
  print("The freakn' stream isn't there");
} else {
  print("All good with the stream");
}

try {
  /*
   * Move all your existing code here...
   */
    print("setting headers");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition","attachment;filename=ebook.pdf");
    response.setHeader("Cache-Control", "no-cache");

    var file:java.io.File = new java.io.File("C:\\Autotrader\\r-gswob.pdf");

    if (file.exists()) {
        print("File Exists");
    } else {
        print("file missing");
    }
    var fileIn:java.io.FileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
    var c:int;
    while ((c = fileIn.read()) != -1) {
            out.write(c);
    }

} catch (e) {
  print("Error generating dynamic PDF: " + e.toString());
} finally {
    if (fileIn != null) {
            fileIn.close();
    }
    if (out != null) {
            out.close();
    }
  facesContext.responseComplete();
}


}]]></xp:this.beforeRenderResponse></xp:view>

Solution

  • Use a greater buffer than just one int value to copy the file:

    var buffer = new byte[10000];
    var len;
    while ((len = fileIn.read(buffer)) != -1) {
            out.write(buffer, 0, len);
    }
    

    This will speed up the download a lot.