tsting
tsting

Reputation: 29

Alfresco upload through REST

I am using a RESTful API for uploading files to Alfresco Share. By using the noderef I can upload files. Is there any way to upload files by folder name?

In our application I have folder called Testing under testing. I have a sub folder called test 1, test 2 etc. This test1 and test2 is generated in Alfresco whenever user registers application by using the Alfresco REST API. But looking up the noderef and nodeid everytime is not a good deal. Could you please provide any feasible solution with sample code for uploading and downloading files by path.

Upvotes: 1

Views: 8780

Answers (3)

raj
raj

Reputation: 29

MultipartFile multipartFile = docData.getBasicDetailsData().getUploadFile(); File file11 = convert(multipartFile);

        HttpPost post = new HttpPost("http://localhost:9090/alfresco/s/cmissamples/upload");

        HttpHost targetHost = new HttpHost("localhost", 9090, "http");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
        AuthCache authCache = new BasicAuthCache();
        authCache.put(targetHost, new BasicScheme());
        final HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file", file11, ContentType.APPLICATION_OCTET_STREAM, "file.ext");
        builder.addTextBody("name",multipartFile.getOriginalFilename());
        builder.addTextBody("path","/CAMUNDA");
        HttpEntity multipart = builder.build();
        post.setEntity(multipart);

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse response = httpClient.execute(post,context);
        logger.info("response.toString(): "+response.toString());

Upvotes: 0

Mardoz
Mardoz

Reputation: 1657

The parameters you need are available on the Alfresco upload script.

POST /alfresco/service/api/upload
  • destination (the folder NodeRef where the node will be created)
  • updateNodeRef (the NodeRef of an existing node that will be updated)
  • siteid and containerid (the Site name and the container in that site where the document will be created)
  • uploaddirectory - name of the folder (either in the site container or the destination) where the document will be uploaded. This folder must already exist

In this case you want to use siteid, containerid, and uploaddirectory. So say I had a site called test with a folder called test1 I would set siteid to test, containerid to documentLibrary, and uploaddirectory to test1. The uploaddirectory should be able to handle your subdirectories.

Here is an example with curl:

curl --form "[email protected]" \
     --form siteid=test \
     --form containerid=documentLibrary \
     --form uploaddirectory=test1 \
      http://localhost:8080/alfresco/service/api/upload?alf_ticket=<your ticket>

Upvotes: 8

Naman
Naman

Reputation: 2203

You can simply use the out-of-the-box webscripts to upload and download files from the repository.

Webscript to upload a content - http://host:port/alfresco/service/cmissamples/upload

Webscript to get the content - http://host:port/alfresco/service/cmissamples/download

To know more about web script - https://wiki.alfresco.com/wiki/Web_Scripts

Hope this helps.

Upvotes: 1

Related Questions