Domino: Read a file attachment without detaching

The function below shows how you can read a file attachment, in this case a text or csv file into a string without detaching it to disk.
Function getAttachmentData

 Dim doc As NotesDocument
 Dim stream As NotesStream
 Dim export As NotesDXLExporter
 Dim mime As NotesMimeEntity
 Dim txt As String
 Dim Start As Double
 Dim Finish As Double
 Dim fileData As String

 On Error Goto processError

 If Not dataDictView Is Nothing  Then Set doc = dataDictView.getDocumentByKey(|ARCHIVE CSV FILE| , True ) 

 If Not doc Is Nothing Then
  Set stream = session.CreateStream
  Set export = session.CreateDXLExporter
  'Convert the document to DXL
  export.setInput doc
  export.setOutput stream
  export.process
  'Process the export into a string file (as long as its less than 2GB)
  txt=stream.ReadText
  txt = Replace(txt, Chr(10), ||)
  'Find the  tags and read what's between them
  Start=Instr(1,txt,||)
  Finish=Instr(1,txt,||)
  fileData=Mid(txt,Start +10, Finish-Start-11)

  'Truncate the stream and write the filedata into it
  stream.Truncate
  stream.WriteText fileData

  'Setup a NotesMIMEEntity to decode the Base64 string
  Set mime = doc.CreateMIMEEntity(|MIMEEntity|)
  mime.SetContentFromText stream, |text/plain;charset=UTF-8|, ENC_BASE64
  mime.DecodeContent
  getAttachmentData = mime.ContentAsText
 End If

 Exit Function

processError:

 currentlog.LogError Err, |Error: | & Err & |: | & Error & | on line | & Erl & | in Agent: getAttachmentData|
 Exit Function

End Function

2 comments:

David said...

Very useful - thanks. I've been looking for a way of getting file contents without extracting the file first.

However, would this work for say a PDF or a Word document? I'm looking for a way of essentially passing new emails to a PHP server via a POST command, including any file attachments.

This is easy enough if the email is mime, but when it's rich text and $FILE fields, it's a lot harder.

Ian Chivers said...

Hi David,

You could have a look at the XML from the output of the DXL Export of the document and see what it looks like, I imagine they'll be there as a Base64 encoded string somewhere. You should be able to convert that back to a byte array in php.

Ian.