How to Add and Remove attachments using MIME

末鹿安然 提交于 2019-12-25 03:27:13

问题


I am trying to remove an attachment that was previously added using MIME. this is my code

try{
    var d = database.getView("Main").getFirstDocument()
    var it = d.getFirstItem("Body")
    var att:NotesEmbeddedObject = it.getEmbeddedObject("mydoc.docx")
    var streamDOC:NotesStream = session.createStream()

    streamDOC.setContents(att.getInputStream())


    var newd;
    newd = database.getView("NewD").getFirstDocument()
    if(newd==null){
        newd = database.createDocument()
        newd.replaceItemValue("Form","Main")
        var me = newd.createMIMEEntity("Body")
    }else{
        var me = newd.getMIMEEntity("Body") 
    }

    var filename = "test.pdf"
    var mc = me.createChildEntity();
    var he = mc.createHeader("Content-Disposition")
    he.setHeaderVal("attachment; filename=\"" + filename + "\"");
    he = mc.createHeader("Content-ID");
    he.setHeaderVal( "<" + filename + ">" );
    mc.setContentFromBytes(streamDOC, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", NotesMIMEEntity.ENC_IDENTITY_8BIT);
    newd.save()
    print("success")
}catch(e){
    print("fail " + e)
}

this is the code I use for deleting the attached files

<xp:repeat id="repeat1" rows="30"
        value="#{javascript:@AttachmentNames()}" var="att"
    >
        <xp:text tagName="p" escape="true" id="computedField1"
            value="#{javascript:att}"
        >
        </xp:text>
        <xp:link escape="true" text="Delete" id="link2">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete"
            >
                <xp:this.action><![CDATA[#{javascript:try{
    var it:NotesRichTextItem = nd.getDocument().getFirstItem("Body")
    var eo:NotesEmbeddedObject = it.getEmbeddedObject(att)
    if(eo==null){
        print("No attachment found")
    }else{
        print("att ok")
        eo.remove()
        nd.save()

    }

        print("success delete " + e)
}catch(e){
    print("fail delete " + e)
}}]]></xp:this.action>
            </xp:eventHandler></xp:link>
    </xp:repeat>

In ytria my document looks like this

When I try to use the link to remove an attachment I get the following errror

2018-11-22 10:27:48 HTTP JVM: fail delete Error calling method 'getEmbeddedObject(string)' on an object of type 'lotus.domino.local.Item [Static Java Interface Wrapper, lotus.domino.local.Item: lotus.domino.Item]'

this is what my webpage look like

What can be the cause of this

Thanks

Thomas

See also this question How to Remove MIME attachments correctly


回答1:


getFirstItem returns a NotesItem, not a NotesRichTextItem. I'm not sure SSJS :NotesRichTextItem casts the result to a NotesRichTextItem. In Java it would be RichTextItem it = (RichTextItem) nd.getDocument().getFirstItem("Body");. Adding (RichTextItem) after the = makes the API convert the result to that class. Maybe that's also needed in SSJS.



来源:https://stackoverflow.com/questions/53428014/how-to-add-and-remove-attachments-using-mime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!