cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading file to alfresco repository using AlfrescoRest API

skiran
Champ in-the-making
Champ in-the-making
Hi all,
I am developing a e-commerce project for magazines. I am using alfresco 3.4.c as Repository for storing content and I am using Jerse client and alfresco REST API to connect to alfresco repository.  In this project, I have to write a Jersey client program to upload content to alfresco repository.
My Jersey Client program is below. In this program, I am uploading Html file, but I want to upload any file (ex: pdf , word file etc).

I used the built in rest service  /alfresco/service/api/upload to upload file into alfresco repository. But it is not uploading content into repository.


    public class AlfrescoFileUpload {
   
   public static void main(String[] args) throws IOException{
   
        ClientConfig config = new DefaultClientConfig();

          Client client = Client.create(config);
         BufferedReader br=null;
         
          WebResource webResource = client.resource("http://127.0.0.1:8888/alfresco/service/api/login");
         
          MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
          queryParams.add("u", "admin");
          queryParams.add("pw", "alfresco");
      
          String s = webResource.queryParams(queryParams).get(String.class);
         
          client.addFilter(new HTTPBasicAuthFilter("admin", "alfresco"));
               
          WebResource wr= client.resource("http://localhost:8888/alfresco/service/api/upload");
                 
          File file=new File("/home/sivakiranm/drag.html");
          
          FormDataMultiPart form = new FormDataMultiPart();
             
           form.field("filedata", "@drag.html");
           form.field("username", "admin");
           form.field("password", "alfresco");
          
            form.field("siteid","gopustak");
           
            form.field("containerid","CompanyHome");
            form.field("uploaddirectory","documentLibrary");
            form.field("filename", "drag.html");
            form.field("noderef","nodeRef=workspace://SpacesStore/6eabdd9c-0489-4d71-8c43-c8ed4c3c6c85");
           // form.field("updatenoderef","alfresco/service/api/node/workspace/SpacesStore/23q1-34fr-5ab4-t4vf/drag.html");
            form.field("description","this is html file");
            form.field("contenttype","application/html");
            form.field("majorversion","1.0");
            form.field("overrite","yes");
            form.field("thumbnails", "yes");
               
               
          form.field("filename", file.getName());
          
           form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE));
         
          //InputStreamProvider response = wr.type(MediaType.MULTIPART_FORM_DATA).post(InputStreamProvider.class, form);
          
         InputStream in=wr.type(MediaType.MULTIPART_FORM_DATA).post(InputStream.class,form);
        
         OutputStream out=new FileOutputStream(form);
           byte buf[]=new byte[1024];
           int len;
           while((len=in.read(buf))>0)
           out.write(buf,0,len);
           out.close();
           in.close();
           System.out.println("\nFile is created………………..");
           }
              
}
}


Can any one help me how to upload file to Alfreco Repository using Rest api?
14 REPLIES 14


1.Using the Node Browser to get the node reference of companyhome ,you can refer to below url on how to use Node Browser
http://docs.alfresco.com/4.1/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Ftasks%2Fadminconsole-n...
2. Pass company home refernce  to destination parameter(the folder NodeRef where the node will be created)
3.Pass "User Homes/Salesforce" to uploaddirectory parameter(name of the folder in the destination where the document will be uploaded).
Following is sample code.

         Part[] parts = {
               new FilePart("filedata", filename, fileobj, filetype, null),
               new StringPart("filename", filename),
               new StringPart("description", description),
               
               new StringPart("destination", "workspace://SpacesStore/ce55a850-b1c7-43c7-a600-b5c58a985429"),
               new StringPart("uploaddirectory", "/TESTFOLDER1")
};

sanketgosavi
Champ in-the-making
Champ in-the-making
How can i get noderef of company home by in javascript without hardcoding

kaynezhang
World-Class Innovator
World-Class Innovator
companyhome itself is a root object,you can use it directly ,why do you want it to be softcoding ?
If you don't want to use it directly ,you can write a javascript funtcion  and in the function use javascript search api to search "company home" node,then return companyhome object

poringe
Champ in-the-making
Champ in-the-making
Hi,

    i want to add 'aspects' and 'aspects value' along with 'upload file'

    then i add this line of java code

    new StringPart("aspects", "ReferenceNumber:ReferenceNumber");

    file is uploaded, aspects is added but how can i add value to aspect?

Sorry for my english

tharun
Champ in-the-making
Champ in-the-making
how to make Multiple files upload from Drupal to Alfresco using CMIS module,
below is my code on settings.php file in drupal to configure sync of drupal and alfresco

$conf['cmis_sync_map'] = array(
   'alfresco_upload' => array(
      'enabled' => TRUE,
      'cmis_folderPath' => '/Sites/beneficiery/documentLibrary/first', // you need to create a site and add a custom foder in it (on Alfresco side)
      //'content_field' => 'field_upload1',
      /*'fields' => array(
        'title' => 'cmis:name'
        //'field_upload1' => 'cmis:file',
        //'field_upload2' => 'cmis:file'
        ),*/

     ),
    'deletes' => TRUE,
  );