gkidd
gkidd

Reputation: 199

Getting attachments from document

This is all I need, nothing too fancy: I'm creating an url from files that have been attached in the document, but the document is not opened. I have an xpage where I want to show attachments from specific document. How do I do this?

Thank you in advance.

Upvotes: 2

Views: 7604

Answers (3)

Lukas
Lukas

Reputation: 402

If you can use Java (like in XPages) then

    import com.ibm.xsp.extlib.util.ExtLibUtil;
    import lotus.domino.MIMEEntity;
    import lotus.domino.Document;
    import lotus.domino.Session;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Serializable;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Vector;
    import lotus.domino.Database;
    import lotus.domino.DocumentCollection;
    import lotus.domino.EmbeddedObject;
    import lotus.domino.Item;
    import lotus.domino.MIMEHeader;
    import lotus.domino.NotesException;
    import lotus.domino.RichTextNavigator;
    import lotus.domino.RichTextItem;
    import lotus.domino.Stream;
    import lotus.domino.View;
    // ...
    private String fileSeparator = File.separator;
    private String tempPath = System.getProperty("java.io.tmpdir") + fileSeparator + "Temp" + fileSeparator;
    // ...
    private void saveFilesFromDoc(Document doc) throws NotesException {
    if (doc.hasEmbedded()) {
        RichTextItem body = null;
        try {
            body = (RichTextItem) doc.getFirstItem("body");
        } catch (ClassCastException e) {
            // save file from MIME (Rich text is converted to MIME)
            MIMEEntity mime = doc.getMIMEEntity();
            findMimeWithFile(mime);
            return;
        }
        if (body != null) {
            // save file from richtext
            RichTextNavigator rtnav = body.createNavigator();
            if (rtnav.findFirstElement(RichTextItem.RTELEM_TYPE_FILEATTACHMENT)) {
                do {
                    EmbeddedObject att = (EmbeddedObject) rtnav.getElement();
                    String fileName = att.getSource();
                    fileName = notConflictFileName(fileName );
                    String path = tempPath + fileName ;
                    att.extractFile(path);
                } while (rtnav.findNextElement());
            }
        } else {
            // ("BODY is NULL");
        }
    }

Get file from richtext converted to Mime

private void findMimeWithFile(MIMEEntity mime) {
    try {
        askMimeForFiles(mime, "");
        MIMEEntity child = mime.getFirstChildEntity();
        while (child != null) {
            askMimeForFiles(child, "child");
            // String encoding = "ISO-8859-2";
            String c = child.getContentType();
            MIMEEntity subChild = child.getFirstChildEntity();
            askMimeForFiles(subChild, "subChild");
            if ("multipart".equals(c)) {
                while (subChild != null) {
                    askMimeForFiles(subChild, "subChild2");
                    // String sc = subChild.getContentType();
                    subChild = subChild.getNextSibling();
                }
            }
            child = child.getNextSibling();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Find out, if MIME Entity is file attachment (or some text)

private void askMimeForFiles(MIMEEntity mime, String prefix) throws NotesException {
    if (mime != null) {
        boolean thisMimeHasFile = false;
        String fileName = "noname";
        Vector<MIMEHeader> headers = mime.getHeaderObjects();
        for (MIMEHeader header : headers) {
            // (prefix + "-header: " + header.getHeaderName() + " :: " + header.getHeaderValAndParams());
            if ("Content-Transfer-Encoding".equals(header.getHeaderName())) {
                if ("binary".equals(header.getHeaderVal())) {
                    thisMimeHasFile = true;
                }
            }
            if ("Content-Disposition".equals(header.getHeaderName())) {
                String val = header.getHeaderValAndParams();
                int odd = val.indexOf("filename=") + "filename=".length();
                int doo = val.length();
                fileName = val.substring(odd, doo);
                this.fileNames.add(fileName);
            }
        }
        if (thisMimeHasFile) {
            safeFilesFromMIME(mime, fileName);
        }
    }
}

If MIME is file attachment, then save it

private void safeFilesFromMIME(MIMEEntity mime, String fileName) throws NotesException {
    Session session = ExtLibUtil.getCurrentSession(); // or user variableResolver
    Stream stream = session.createStream();
    String pathname = tempPath + fileName;
    stream.open(pathname, "binary");
    mime.getContentAsBytes(stream);
    stream.close();
}

Upvotes: 1

Grant Lindsay
Grant Lindsay

Reputation: 159

I found that DominoDocument.AttachmentValueHolder.getHref() works for getting the URL to an attached file or image, if you have a handle to the document. For example:

<xp:image
   id="image1">
   <xp:this.url>
      <![CDATA[#{javascript:document1.getAttachmentList("Body").get(0).getHref()}]]>
   </xp:this.url>
</xp:image>

You would need to handle multiple attachments by iterating over the elements returned from getAttachmentList().

Upvotes: 7

stwissel
stwissel

Reputation: 20374

The easiest way is to use @AttachmentNames (in a view column) to get the names of the files. Then you can construct the url using db.nsf/0/unid/$file/[filename] -- that's classic, won't run in XPiNC. There is a second URL syntax (need to check) that is XPages specific:

http(s)://[yourserver]/[application.nsf]/xsp/.ibmmodres/domino/OpenAttachment/[application.nsf]/[UNID|/$File/[AttachmentName]?Open

Read my full article on it here: http://www.wissel.net/blog/d6plinks/SHWL-86QKNM

(includes SSJS sample)

Upvotes: 6

Related Questions