To download attachments in an application and also to be able to delete them, the FileDownload control is used on IBM Domino XPages. Unfortunately, this control comes with a little twist. If you click on the trash can icon to delete the attachment, it looks like the file is actually deleted. But the file is not finally deleted until the XPage is saved.
If you leave the XPage after the click on the trash can icon, without saving the document – after all, the attachment disappeared from the view, so why SHOULD you save the document anyway – and open it some time later again, the file is back.
Img. 1: Document including several attachments
Img. 2: Document after clicking the trash can icon of the last attachment
Img. 3: The last attachment is back again after leaving the document without saving and coming back to it afterwards
In order to execute the change, or rather the deletion, directly within the document, it is necessary to add the property setSave(true) to the event handler of the trash can icon. Unfortunately, you cannot do this within the Domino Designer. So a little programming work is needed.
Luckily I have found the appropriate code for most of this work in a discussion comment on stackoverflow by Sven Hasselbach: http://stackoverflow.com/questions/13101615/auto-save-doc-after-delete-of-attachment-in-file-download-control
Unfortunately, the original code always produced the following error when I used it with Notes 9:
X XPAGE ERROR: COM.XSP.IBM.ACTION.ACTIONGROUP INCOMPATIBLE WITH COM.IBM.XSP.ACTIONS.DELETEATTACHMENTSACTION
Therefore, I adjusted the source code, so that it works with Notes 8.5.3 and Notes 9 alike. To use it properly you only need to insert it into the BeforeRenderResponse event of the XPage.
<xp:this.beforeRenderResponse>
<![CDATA[#{javascript:
function overrideFileDownloadAction( fDownload ){
//Check whether the control exists or not
if( fDownload === null )
return;
//Call actual function
rekOverrideFileDownloadAction( fDownload, fDownload );
}
/*
*This control iterates through all children of the FileDownload control
*and seeks the event handler
*/
function rekOverrideFileDownloadAction( component:javax.faces.component.UIOutput,fDownload:com.ibm.xsp.component.UIFileDownload ){
try{
//Get children of the current element
var children:java.util.List = component.getChildren();
var it:java.util.Iterator = children.iterator();
var curChild:javax.faces.component.UIOutput;
//Loop over all children
while( it.hasNext() ){
curChild = it.next();
if( typeof( curChild ) === 'com.ibm.xsp.component.xp.XspEventHandler' ){
//Event handler found
//set setSave to true
// to automatically save after the event
curChild.setSave(true);
}
//Event handler not found, therefore recursive call to investigate the children
rekOverrideFileDownloadAction( curChild , fDownload );
}
}catch(e){}
}
overrideFileDownloadAction( getComponent( 'IDFILEDOWNLOAD' ) );
//IDFILEDOWNLOAD = the ID of the FileDownload control
}]]>
</xp:this.beforeRenderResponse>
If the above code is inserted into your XPage, the file is automatically saved after clicking on the trash can icon. The disadvantage of this is that it saves the complete document and not just delete the attachment. Therefore, you have to be careful to ensure that you have not previously changed the value of another field that should not be saved.