cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading files using RESTful

elimarcos
Champ in-the-making
Champ in-the-making
Hi,

I am using the following approach to perform uploading files to the Alfresco repository:

URL: http://localhost:8080/alfresco/service/api/upload/?alf_ticket = {User's ticket}
parameters: [filedata, filename, description, siteid, containerId]

However, this approach does not allow me to insert files in specific subdirectories.

I need to perform file uploads via RESTful api using the uuid of the target folder.

Does anyone have any tips how to do this?

Alfresco Version: 4.2.c

ps. Sorry for my english!

Grateful.
18 REPLIES 18

kaynezhang
World-Class Innovator
World-Class Innovator
try this

URL: http://localhost:8080/alfresco/service/api/upload/?alf_ticket = {User's ticket}

File file = new File("C:/Test_Upload.pdf");
String filetype = "application/pdf"
String filename="Test_Upload.pdf";

HttpClient client = new HttpClient();
PostMethod post = new PostMethod(URL);
Part[] parts = {
   new FilePart("filedata", filename, file, filetype, null),
   new StringPart("filename", filename),
   new StringPart("description", "description"),
   new StringPart("siteid", "yoursite"),
   new StringPart("containerid", "documentLibrary"),
   // your can add more paramter here
   //new StringPart("uploaddirectory", "documentLibrary"),
   //new StringPart("updatenoderef", "updatenoderef"),
   //new StringPart("contenttype", "contenttype"),
   //new StringPart("aspects", "aspects")
   …..
   };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();

fahad
Champ in-the-making
Champ in-the-making
Dear kaynezhang

how you are managing your upload file code. The java script restricts "transaction required". First But your code is without Transaction.
second, I managed to add transaction but not succeeded..Can you tel me some way to do that.
   try
           {
               userTransaction1.begin();
              
             
             post.setRequestEntity(new MultipartRequestEntity(parts, post
                   .getParams()));

             int status = client.executeMethod(post);

             System.out.println(post.getResponseBodyAsString());

             post.releaseConnection();
              
          
               userTransaction1.commit();
           }
           catch(Exception e)
           {
               try { userTransaction1.rollback(); } catch (IllegalStateException ee) {}
               throw e;
           } 

Can you answer how you manage it without transaction ??

iblanco
Confirmed Champ
Confirmed Champ
The API you are using is the same that the Drag & Drop feature of Alfresco Share uses and it can upload content to any folder.
Inspecting the POST with Chrome's developer tools I see that this API gets a "destination" parameter with the NodeReference of the destination folder, so if you can get the NodeRef of your destination folder you should be able to send content there.

Furthermore, if you check this API's documentation (http://localhost:8080/alfresco/service/script/org/alfresco/repository/upload/upload.post) it seems like it should even be able to accept and "uploadDirectory" parameter that refers to a destination folder by path name.

iblanco
Confirmed Champ
Confirmed Champ
You might also find interesting this reference in the wiki:

http://wiki.alfresco.com/wiki/Web_Scripts_Examples#File_Upload

With this as an example you could even build your own webscript for upload and implement the logic you prefer to decide where to put the content.

elimarcos
Champ in-the-making
Champ in-the-making
Hi,

Anyway, I could understand what I was doing wrong!

Using the example of kaynezhang and checking API suggested by iblanco, I found that the parameter "siteid" deletes the parameter "destination".

I was trying to use both, so it was not working. It was just remove the "siteid" that the parameter "destination" started to function as it should.



Part[] parts = {
   new FilePart("filedata", filename, file, filetype, null),
   new StringPart("filename", filename),
   new StringPart("description", "description"),
   // new StringPart("siteid", "yoursite"),
   // new StringPart("containerid", "documentLibrary"),
        new StringPart("destination", "workspace://SpacesStore/7118e242-86b1-489a-ac2e-193364ccf6e4"),
}



Thank you for everything!

fahad
Champ in-the-making
Champ in-the-making
Dear

In your code you mentioned new StringPart("destination", "workspace://SpacesStore/7118e242-86b1-489a-ac2e-193364ccf6e4")
I am not able to get the destination value, how to get it???
your detailed explanation can really help?

kaynezhang
World-Class Innovator
World-Class Innovator
Yes if siteId is not null ,upload webscript works in site mode ,the content will be upload in a destination node which is  in a container(specified by containerId parameter) under the site(specified by siteId parameter).
If siteId is null, upload webscript works in non site mode.the content will be upload in a destination node specified by destination parameter.

pyt
Champ on-the-rise
Champ on-the-rise
Hey,
i'd like to create a folder via RESTful API. Is it in anyway possible, to use the code that kaynezhang wrote to create a document for my case?

What I am actually trying to do is to create a folder using the "DEV HTTP CLIENT" in chrome browser.
These are my inputs:

URL:

POST localhost:8080/alfresco/service/api/node/workspace/SpacesStore/15fc9618-56a5-4037-ac25-508899d5ba28/children


These are the parameters i send with:

{"alf_destination":"workspace://SpacesStore/15fc9618-56a5-4037-ac25-508899d5ba28","prop_cm_name":"test","prop_cm_title":"","prop_cm_description":""}


Every time i send the request, i recive a response saying: 400 Bad Request

What do I have to change?

Best regards
pyt

iblanco
Confirmed Champ
Confirmed Champ
This API is for file upload, not for directory creation.

Share creates folder by calling the form processor: http://localhost:8080/alfresco/service/api/type/cm%3afolder/formprocessor

And POST somethin like:
{"alf_destination":"workspace://SpacesStore/90c7ba28-b991-48db-8ba3-f42a198dd54e","prop_cm_name":"MyFolder","prop_cm_title":"","prop_cm_description":""}

There might be a better API to do this, but this should also work.