• Anonymous
  • Login
  • Register
Code Library - Discussion: In-Memory Attachment via MIME

Created on Dec 9, 2006
Created by Michael Zischeck
Status
Open

Hi

I found that to attach file in-memory is quite easy using MIME.

The thing is when you have an application which should attach files on the server from another source you can use MIME if you do not have the file as a file but rather as an array of bytes ready:

here some sample code (where here the file is available on C drive, normally I would get it via FTP/WS or another Notes Document)

// creating the output stream used to create the MIME attachments
Stream outStream = session.createStream();

session.setConvertMIME(false);
// create the MIME body
MIMEEntity body = docCurrent.createMIMEEntity();

String files[] = new String[4];
files[0] = "c:\\temp\\1.jpg";
files[1] = "c:\\temp\\2.pdf";
files[2] = "c:\\temp\\3.doc";
files[3] = "c:\\temp\\4.xls";

// Get the input file
for( int i = 0; i < files.length; i++) {
// creating the stream to read the files this is the INPUT stream
Stream inStream = session.createStream();
inStream.open(files[i], "binary");

// reading the stream into the OUTPUT stream
do {
byte[] buffer = inStream.read(32767);
outStream.write(buffer);
} while (!inStream.isEOS());
inStream.close();

// create a child for each attachment
MIMEEntity child = body.createChildEntity();

// find the fileSuffix
String fileSuffix = files[i].substring(files[i].lastIndexOf(".")+1);

// set the child to the outstream using a mapped MIME type
// MIME type mapping see: http://www.w3schools.com/media/media_mimeref.asp
child.setContentFromBytes(outStream, mapMIMEType(fileSuffix), MIMEEntity.ENC_IDENTITY_BINARY);

// set name for file attachment
MIMEHeader header = child.createHeader("Content-Disposition");
header.setHeaderVal("attachment; filename=\"" + files[i] + "\"");

// set unique id for file attachment to be able to refer to it
header = child.createHeader("Content-ID");
header.setHeaderVal(files[i]);

outStream.truncate();
}
outStream.close();

docCurrent.save( true, true );

Hope someone likes this. It helped me in a very important project where we would use a WS to deliver a fileattachment which needed being attached on the server ( where we do no t have write access) to a document.

Michael



Documents
     
Creation Date
Author
Subject
Dec 9, 2006 Michael Zischeck In-Memory Attachment via MIME
Dec 11, 2006 Marko Bonaci     Response: Wrong place to post?
     
In this field you can enter the actual discussion topic.

You can use the rich text editor for rich text formating. You can also enter HTML to embed objects, e.g. to embed a YouTube video or a screenshot of the project. In this case use '[' and ']' to mark the passthrough HTML as such.

Please note that the first time you use the new UI your description is converted from rich text to MIME. You might want to copy and paste the raw plain text from the old UI in the new UI so that you don't loose information.