从文档中获取附件

时间:2012-02-15 10:37:27

标签: lotus lotus-domino xpages

这就是我所需要的,没有什么太花哨的: 我正在从文档中附加的文件创建URL,但文档未打开。我有一个xpage,我想显示特定文件的附件。我该怎么做?

提前谢谢。

3 个答案:

答案 0 :(得分:6)

最简单的方法是使用@AttachmentNames(在视图列中)来获取文件的名称。然后你可以使用db.nsf / 0 / unid / $ file / [filename]构建url - 这是经典的,不会在XPiNC中运行。还有第二个URL语法(需要检查)是特定于XPages的:

HTTP(S):// [yourserver] / [application.nsf] /xsp/.ibmmodres/domino/OpenAttachment/ [application.nsf] / [UNID | / $文件/ [ATTACHMENTNAME]打开

请在此处阅读我的完整文章:http://www.wissel.net/blog/d6plinks/SHWL-86QKNM

(包括SSJS样本)

答案 1 :(得分:5)

如果您有文档句柄,我发现DominoDocument.AttachmentValueHolder.getHref()用于获取附加文件或图像的URL。例如:

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

您需要通过迭代从getAttachmentList()返回的元素来处理多个附件。

答案 2 :(得分:1)

如果你可以使用Java(比如XPages)那么

    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");
        }
    }

从richtext转换为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();
    }
}

找出,如果MIME实体是文件附件(或某些文本)

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);
        }
    }
}

如果MIME是文件附件,则保存

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();
}