Hi,

This should generally work.
Here is a typical Groovy script we often use to add attachments from 
File Uploads
We call it this way for example:

    #set($doctitle = $xwiki.getUniquePageName("Space", "title"))
    #set($newdoc = $xwiki.getDocument("Space.${doctitle}"))
    ## attach files if necessary
    #set($attachgroovy = $xwiki.parseGroovyFromPage("XWiki.UploadGroovy"))
    #if($attachgroovy == "groovy_missingrights")
      #warning("no prog rights warning")
    #else
      #set($nb = $attachgroovy.addAttachments($newdoc, $context))
      #if($nb == -10)
        #warning("no prog rights warning")
      #end
   $newdoc.save() or $newdoc.saveWithProgrammingRights().
  #end



---
import com.xpn.xwiki.doc.*;
import com.xpn.xwiki.*;
import com.xpn.xwiki.util.Util;
import java.io.*;
import java.util.*;

public class Ajout {
   public int addAttachments(doc1, context1) {
       def doc = doc1.document;
       if (!context1.hasProgrammingRights())
         return -10;
       def context = context1.context;
       if (context==null)
        return -10;
       def xwiki = context.getWiki();
       int nb = 0;
       def fileupload = xwiki.getPlugin("fileupload",context)
       for (fileitem in fileupload.getFileItems(context)) {
        if (!fileitem.isFormField()) {
        def name = fileitem.fieldName
        byte[] data = fileupload.getFileItemData(name, context);
        if ((data!=null)&&(data.length>0)) {
        String fname = fileupload.getFileName(name, context);
        int i = fname.lastIndexOf("\\");
        if (i==-1)
           i = fname.lastIndexOf("/");
        def filename = fname.substring(i+1);
        filename = filename.replaceAll("\\+"," ");
        def attachment = doc.getAttachment(filename);
        if (attachment==null) {
          attachment = new XWikiAttachment();
          doc.getAttachmentList().add(attachment);
          // Add the attachment to the document
          attachment.setDoc(doc);
        }
        attachment.setContent(data);
        attachment.setFilename(filename);
        // TODO: handle Author
        attachment.setAuthor(context1.user);
        doc.setAuthor(context1.user);
        doc.setCreator(context1.user);
        doc.saveAttachmentContent(attachment, context);
        nb++;
         }
        }
       }
      return nb;
     }
}

Ajdin Brandic a écrit :
> I'm executing some code on Save which attaches a PNG file to a document
> that was edited/saved.
>  
> When I call docObj.saveAttachmentContent(attP, context); the attachment
> is save OK but any changes made to my document (ie. text changes) are
> not saved.
> If I comment out this line the text changes are saved, but off course
> not the attachment.
>  
> Is this normal behaviour, what do the developers think?
>  
> def imgAsBites =
> xwiki.getURLContentAsBytes("http://www.websequencediagrams.com/index.php
> "+umlImage, context);                           
> def attP = docObj.getAttachment(filenameToSavaAs);
> if(!attP)
>           {
>             attP = new
> com.xpn.xwiki.doc.XWikiAttachment(docObj,filenameToSavaAs);
>             docObj.getAttachmentList().add(attP);
>             println "NO Attach";
>           }   
> attP.setContent(imgAsBites);       
> docObj.saveAttachmentContent(attP, context); 
>  
> Regards
> Ajdin
>  
>  
> --------------------------------------------------------
>
> NOTICE
>
> This message and any files transmitted with it is intended for the addressee 
> only and may contain information that is confidential or privileged. 
> Unauthorised use is strictly prohibited. If you are not the addressee, you 
> should not read, copy, disclose or otherwise use this message, except for the 
> purpose of delivery to the addressee. 
>
> Any views or opinions expressed within this e-mail are those of the author 
> and do not necessarily represent those of Coventry University.
> _______________________________________________
> users mailing list
> users@xwiki.org
> http://lists.xwiki.org/mailman/listinfo/users
>
>   


-- 
Ludovic Dubost
Blog: http://blog.ludovic.org/
XWiki: http://www.xwiki.com
Skype: ldubost GTalk: ldubost

_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to