Hi Tobias,

Here's some code to create an image node programmatically in magnolia 4.4.x --> 
maybe you can adapt it for magnolia 4.5...
Typically, you'd call this in your model-class' execute() method, after 
checking the input parameters...

public void uploadImage(){
                MultipartForm f = MgnlContext.getPostedForm(); // remember to 
set your form encoding correctly
                final Document doc = f.getDocument("uploadField"); // form 
field containing uploaded file
                
                // now write the uploaded image with superuser priveleges
                MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
                        @Override
                        public void doExec() {
                                final Content parentFolder = getUploadFolder(); 
// get the target folder as a content object
                                if (parentFolder==null){
                                  // TODO error handling!
                                        return;
                                }
                                try {
                                        
synchronized(ExclusiveWrite.getInstance()){

                                                String validName = 
Path.getUniqueLabel(parentFolder, "upload"); // eg 'upload05' will be the 
node-name
                                                Content uploadNode = 
parentFolder.createContent(validName, ItemType.CONTENTNODE);

                                                // TODO set other fields, 
depending on the information you ask the user in the upload-form
                                                
//uploadNode.setNodeData("description", getDescription());
                                                
                                                // save the document
                                                
SaveHandlerImpl.saveDocument(uploadNode, doc, "document", null, null);
                                                
                                                parentFolder.save();  // call 
content.save() on the parent node!
                                        }
                                } catch (RepositoryException e) {
                                  // TODO error handling!
                                }
                        }
                });             
        }


Regards from Vienna,

Richard

-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] 
Im Auftrag von Tobias Reinhardt (via Magnolia Forums)
Gesendet: Dienstag, 26. Juni 2012 17:33
An: Magnolia User List
Betreff: [magnolia-user] Re: Create an image node programmatically in 4.5.x

I did forget to add an 
[code]session.save();[/code]
to the pasted snippet.

-- 
Context is everything: 
http://forum.magnolia-cms.com/forum/thread.html?threadId=3c12a79f-6909-469c-9f93-2f982920aabc


----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------





----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to