Replace attachment when uploading a new attachment

/***
 * replaceAttachment
 *
 * replaces an existing attachment in a richtext item with a newly uploaded attachment.
 * Only one attachment will be left in the richtext item.
 *
 * @author Sven Hasselbach
 * @param ds DataSource document which contains the target rich text item
 * @param fileUploadControlId id of the file upload control
 * @param rtItemName Name of target richtext item
 *
 ***/
function replaceAttachment( ds:NotesXspDocument, fileUploadControlId:String, rtItemName:String ){
   var con = facesContext.getExternalContext();
   var request:com.sun.faces.context.MyHttpServletRequestWrapper = con.getRequest();
   var map:java.util.Map = request.getParameterMap();
   var fileDataName = getClientId( fileUploadControlId ) ;
   var fileData:com.ibm.xsp.http.UploadedFile = map.get( fileDataName );
	
   if( fileData == null ){
      return;
   }

   var tempFile:java.io.File = fileData.getServerFile();
   var correctedFile = new java.io.File( tempFile.getParentFile().getAbsolutePath() + 
   java.io.File.separator + fileData.getClientFileName() ); 

   var success = tempFile.renameTo(correctedFile);
   try{
      ds.removeAllAttachments( rtItemName );
   }catch(e){}

   var rtFiles:NotesRichTextItem = null;
   if(!(ds.getDocument().hasItem( rtItemName ))){
      rtFiles = ds.getDocument().createRichTextItem( rtItemName )
   }else{
      rtFiles = ds.getDocument().getFirstItem( rtItemName);
   } 

   rtFiles.embedObject(lotus.domino.local.EmbeddedObject.EMBED_ATTACHMENT, "",
      correctedFile.getAbsolutePath(), null); 
   correctedFile.renameTo(tempFile);

   ds.save();
}





This function will replace an existing attachment in a richtext item with a newly uploaded attachment.
Only one attachment will be left in the richtext item.

Just call the function in the SSJS code in a submit button: replaceAttachment( document1, 'fileUpload1' , 'Body' )

See here for more details:
http://stackoverflow.com/questions/9925666/replace-attachment-when-uploading-a-new-attachment


Here is an example XPage (without the function, you have to copy it by yourself to the button code):

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

  <xp:this.data>
     <xp:dominoDocument var="document1" formName="TestRT"
        action="editDocument" documentId="2D8A8AB0DD77C895C12579E4004E594A">
     </xp:dominoDocument>
  </xp:this.data>

  <xp:fileUpload id="fileUpload1" value="#{document1.Body}"></xp:fileUpload>
  <xp:br></xp:br>
  <xp:br></xp:br>
  <xp:fileDownload rows="30" id="fileDownload1" displayLastModified="false" value="#{document1.Body}"></xp:fileDownload>
  <xp:br></xp:br>
  <xp:br></xp:br>
  <xp:button value="Label" id="button1">
     <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete" immediate="false" save="true">
           <xp:this.action>
              <![CDATA[#{javascript:replaceAttachment( document1, 'fileUpload1' , 'Body' )}]]>
           </xp:this.action>
     </xp:eventHandler>
  </xp:button>

</xp:view>

JavaScript (Server)
Sven Hasselbach
April 20, 2012 5:53 AM
Rating
669

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...