0

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

enter image description here

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

enter image description here

What can be the cause of this

Thanks

Thomas

See also this question How to Remove MIME attachments correctly

Thomas Adrian
  • 3,543
  • 6
  • 32
  • 62

1 Answers1

0

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.

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • The cast in Java would only work if the item is already composite data, which it’s not here. I think that the `DominoDocument` class used by document data sources has some specialized methods for manipulating attachments in a neutral way. – Jesse Gallagher Nov 22 '18 at 11:02
  • I solved by using var eo = nd.getDocument().getAttachment(att) instead. but now I have another problem., when all attachment is removed all the $File items have been removed but the Body items are still in the document. so when I add a new attachment I get all attachments back – Thomas Adrian Nov 22 '18 at 12:40
  • I added a new question about this https://stackoverflow.com/questions/53431447/how-to-remove-mime-attachments-correctly – Thomas Adrian Nov 22 '18 at 12:52