Nov 26, 2012 6:27 PM
35 Posts
topic has been resolvedResolved

Create HTML file from computed field content

  • Category: Other
  • Platform: Windows
  • Release: 8.5.3
  • Role: End user,Developer,Administrator
  • Tags:
  • Replies: 8
Hello forum, 
 
I'm a little bit stuck with the next issue: 
 
I would like to create a HTML file from the content of a computed field that will automaticaly download when the user push a button on my xpage, I tried working on next code, but it's not really working...
 
Hope someone would be able to help me. 
 
Thx in advance for your help. 
 
=============================== 
 
// Load the java packages
importPackage(java.io);
 
// Put Component HTML content into Var 
var htmlCode = getComponent("cfHtmlCode").getValue(); 
 
// generate CSVPrinter Object
var BAOS:ByteArrayOutputStream = new ByteArrayOutputStream();
var csvWriter:OutputStreamWriter = new OutputStreamWriter(BAOS);
 
// Open the servlet Response
var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
 
//setting response headers for browser to recognize data
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentLength(BAOS.size());
response.setHeader( "Content-Disposition", "attachment; filename=\"test.html\"" );
 
// get a handle on the actual output stream and write the Workbook
var servletOutputStream = response.getOutputStream();
BAOS.writeTo(servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();
 
// Close the renderer and return the document to the browser
facesContext.responseComplete();
Nov 27, 2012 5:26 AM
135 Posts
Re: Create HTML file from computed field content
 You are not using your htmlCode variable at all?
Nov 27, 2012 10:52 AM
35 Posts
Re: Create HTML file from computed field content
 Thx Panu,
 
Well, I have no idea how to do and where to use my variable. Everything what I tried failed... 
 
I probably need to write to content of my htmlCode variable into the BAOS, but I just don't know how ! 
 
thx for your assistance 
Nov 27, 2012 12:49 PM
135 Posts
Re: Create HTML file from computed field content
Your code seems complex. Have you tried this way:
 
http://www.wissel.net/blog/d6plinks/SHWL-7MGFBN
Nov 27, 2012 1:24 PM
366 Posts
Re: Create HTML file from computed field content
 I think what you are looking for is an "XAGent" that will output html to the browser after an event.
 
 On your button add the code to the onClick event to load the target XAgent
 
context.redirectToPage("HTMLXPage.xsp"); 
 
 
On the target XPage, set it's render property to false, and in the afterRenderResponse event add the following code.  Where I have the Hello World output you can compute whatever output you want.
 

var extCont = facesContext.getExternalContext(); 


// The Faces Context Response Writer is similar to the Lotuscript print statement 

// and the Java printwriter object to produce output to the browser.

// There is also a getResposneStream() for producing binary output.

var pageOutput = facesContext.getResponseWriter();


// The servlet's response object provides control to the response object.

var pageResponse = extCont.getResponse();


// The content type has to be set to the output MIME type.  In this case HTML.

pageResponse.setContentType("text/html");

// Additional methods of the response object can be set.  The following keeps the content from being cached.

pageResponse.setHeader("Cache-Control", "no-cache");


// Use the writer object to produce all of the output.  The following is a simple HTML Page.

pageOutput.write("<h1>Hello World</h1>");

// End and close the writer object.

pageOutput.endDocument();


// Terminate the request processing lifecycle.

facesContext.responseComplete(); 
Nov 27, 2012 3:28 PM
35 Posts
Re: Create HTML file from computed field content
 Thx Panu, Paul,
 
I will take a closer look at it tonight, is the code going to create the HTML file that will be prompted to the user for download? 
 
Because that's what I'm looking for and I don't see the Filename in the code, maybe that user will be presented to enter a Filename, but if we can force a Filename, this would be great. 
 
thx
Nov 27, 2012 4:12 PM
135 Posts
Re: Create HTML file from computed field content
Set the file name just like in your code.
Nov 27, 2012 4:47 PM
35 Posts
Re: Create HTML file from computed field content
 Hi guys,
 
This is wonderfull, it's really working like a charm... 
 
I will now go further and try to pass my htmlCode and filename variable to use to this new xpage. (any tips would be great)
=> In the meantime I found a solution using sessionScope ! 
 
Any idea if I also can pass an attachment (jpeg picture to be downloaded too) ? My HTML file is refering to a jpg file that I also want to download, can the same xpage trigger a multiple file download? 
 
Thx very much for your quick assistance. 
 
Best regards, 
 
Chris 
Nov 27, 2012 6:33 PM
366 Posts
Re: Create HTML file from computed field content
 You can not have multiple "streams" from an XAgent by default.  There is only one output stream that is either text or binary.