user4372614
user4372614

Reputation: 25

Poi4Xpages - write file to document during postGeneration

as per a suggestion recently received from Stwissel, I am using the following code to convert a file to mime and attach to a notes document during the post generation process of POI4Xpages.

EDITED NEW CODE:

The following code attaches to document, but throws an error: 502 Bad Gateway - The server returned an invalid or incomplete response.

public void saveExcel(Workbook a, Document newDoc) throws NotesException, IOException{

    newDoc.replaceItemValue("Form", "Provider");

    // Create the stream
    Session session = DominoUtils.getCurrentSession();
    Stream stream = session.createStream();
    // Write the workbook to a ByteArrayOutputStream
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    a.write(bos);
    // Convert the output stream to an input stream
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    stream.setContents(is);



MIMEEntity m = newDoc.createMIMEEntity("body");
MIMEHeader header = m.createHeader("content-disposition");
header.setHeaderVal("Mime attachment");
m.setContentFromBytes(stream, "application/vnd.ms-excel", MIMEEntity.ENC_IDENTITY_BINARY); 
m.decodeContent();
newDoc.save(true, true);
}

ORIGINAL CODE:

var stream:NotesStream = session.createStream();

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "Provider");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Subject");
header.setHeaderVal("MIME attachment");
if (stream.open("c:\\notes\\data\\abc.xlsx", "binary")) {
    if (stream.getBytes() != 0) {
        body.setContentFromBytes(stream, "application/vnd.ms-excel",
        NotesMIMEEntity.ENC_IDENTITY_BINARY);
    } else requestScope.status = "File was not found.";
} else requestScope.status = "Could not open file.";
stream.close();
doc.save(true, true);
// Restore conversion
session.setConvertMIME(true);

However, this code is only attaching a file which is already stored on the server's local directory. How can I get this code to take the POI fileOutputStream and attach that?

Upvotes: 0

Views: 359

Answers (1)

muenzpraeger
muenzpraeger

Reputation: 373

It's a mix of what what Knut and I've commented about. The important part is that you'll use the write() method to pass the workbook data to an output stream.

// Create the stream
Stream stream = session.createStream();
// Write the workbook (you haven't clarified) to a ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
// Convert the output stream to an input stream
InputStream is = new ByteArrayInputStream(bos.toByteArray());
stream.setContents(is);

Upvotes: 1

Related Questions