cancel
Showing results for 
Search instead for 
Did you mean: 

Send image file to alfresco server with Webservices

anurak
Champ in-the-making
Champ in-the-making
I have a problem when created a new content(image) to alfresco server with webservices. The content showed in alfresco but when clicked on an image it did not show correctly. I try to check from the forums but can not found any information. Can i send any image to alfresco server with webservices or not? Below is my code, everything fine when i run.

======== CODE ==========
        ContentFormat contentFormat = new ContentFormat("image/jpeg", null);

        NamedValue[] properties = new NamedValue[]{new NamedValue(ContentModel.PROP_NAME.toString(), name)};
        CMLCreate create = new CMLCreate("1", parentReference, ContentModel.TYPE_CONTENT.toString(), properties);
        CML cml = new CML();
        cml.setCreate(new CMLCreate[]{create});
        UpdateResult[] result = getRepositoryWebService().update(cml);    
       
        Reference newContentNode = result[0].getDestination();
       
        File file = new File("C:\\Documents and Settings\\admin\\My Documents\\DSC01989.JPG");
       
        FileInputStream in  = new FileInputStream(file);
        int size=(int)file.length();
        byte[] bytes=new byte[size];
        in.read(bytes);
        Content content = contentService.write(newContentNode, ContentModel.PROP_CONTENT.toString(), bytes, contentFormat);
=========================================

Thanks,

Anurak
5 REPLIES 5

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

I've performed some tests and this issue has already been fixed.

You should be able to send any binary content to the repository via the web service API.

Cheers,
Roy

anurak
Champ in-the-making
Champ in-the-making
Roy,

I used alfresco 1.1.1, this version its already fixed or not? What wrong with my code? Can you post the code that you tested here?

Thanks for your reply,
anurak

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

Here is some sample code I added to sample3 in order to double check your use case …


           // Upload binary content into the repository
            Reference reference = WebServiceSample2.executeSearch();
            ParentReference parentReference = new ParentReference(ASSOC_CONTAINS, ASSOC_CONTAINS);
            parentReference.setStore(reference.getStore());
            parentReference.setUuid(reference.getUuid());
           
            // Create the content
            NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "test.jpg")};
            CMLCreate create = new CMLCreate("1", parentReference, Constants.TYPE_CONTENT, properties);
            CML cml = new CML();
            cml.setCreate(new CMLCreate[]{create});
            UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);    
           
            // Get the created node and create the format
            Reference newContentNode = result[0].getDestination();             
            ContentFormat format = new ContentFormat("image/jpeg", "UTF-8"); 
           
            // Open the file and convert to byte array
            InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
            byte[] bytes = ContentUtils.convertToByteArray(viewStream);
           
            // Write the content
            WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);

Essentially the same as you have been doing, except that you'll notice a couple of classes you won't have (WebServiceFactory, ContentUtils).  These have been added in 1.2, along with some other classes, to help simplify using the web service API from a Java client.

The fix went in on the 18th of Jan …


————————————————————————

r2133 | royw | 2006-01-18 11:25:06 +0000 (Wed, 18 Jan 2006) | 2 lines

Changed paths:

   M /alfresco/HEAD/root/projects/remote-api/source/java/org/alfresco/repo/webservice/content/ContentWebService.java


So it isn't in 1.1.1 and I don't think it made it into 1.1.2, but 1.2 RC1 is due for release this week so you pick it up then.  Alternatively you can get the fix directly out of SVN.

Hope this helps,
Roy

anurak
Champ in-the-making
Champ in-the-making
Thanks Roy, i will try it.
anurak

anurak
Champ in-the-making
Champ in-the-making
Hi Roy,

I was test with 1.2 RC1 and it worked.
Thanks for your help

anurak