PRVS
PRVS

Reputation: 1690

Change content of file - Alfresco

I have an Custom Document Library Action to Alfresco files, and when I press this button opens a new page with an applet (javascript) to make changes to a file, but I'm doing the modifications in base64 and to "appear" on the screen with this :

var stringPDF = "<object data=\"data:application/pdf;base64," +
JSON.parse(pdfbase64).message + "\"
type=\"application/pdf\"width=\"100%\"
height=\"100%\"></object>";$("#pdfTexto").html(stringPDF);

But I really need is to change the file, for when the repository again, there have to change, not just display. How do I change the existing file's contents to the new with the change?

I use this URL to make GET of the file:

http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/21384098-19dc-4d3f-bcc1-9fdc647c05dc/latexexemplo.pdf

Then I convert to the base64... And I make the changes...

But if I want to make a POST to change the content, how can I make this?

Thanks in advance.

Upvotes: 2

Views: 2134

Answers (2)

Younes Regaieg
Younes Regaieg

Reputation: 4156

As I mentionned in my response to this question :

The fastest and easiest way to achieve that is to leverage the RESTfull API

This will also ensure compatibility with new versions of alfresco.

Note that you need to provide the noderef for the document to update in the form property updatenoderef and that the property majorversion is a boolean flag to specify if the new version is a minor/major version of the document.

Here is a sample code that might help you with your usecase:

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost uploadFile = new HttpPost(<alfresco-service-uri>+"/api/upload?alf_ticket="+<al-ticket>);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("username", "<username>", ContentType.TEXT_PLAIN);
        builder.addTextBody("updatenoderef", <noderef>, ContentType.TEXT_PLAIN);
        builder.addTextBody("...", "...", ContentType.TEXT_PLAIN);
        builder.addBinaryBody("filedata", <InputStream>, ContentType.DEFAULT_BINARY, <filename>);
        HttpEntity multipart = builder.build();

        uploadFile.setEntity(multipart);

        CloseableHttpResponse response = httpClient.execute(uploadFile);

        String responseString = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); 
        JSONObject responseJson = new JSONObject(responseString);
        if (response.getStatusLine().getStatusCode()!=200){
            throw new Exception("Couldn't upload file to the repository, webscript response :" + responseString );
        }

Note 1: You need to replace these tockens <*> with your own values/vars

Note 2: If you have problem retrieving a ticket, check this link, or this one

Note 3: To do this in JavaScript instead of java, visit this link and try to use js to post the parameters I referred as instructed !

Note 4: Since you are working on share, you are most probably authenticated.

If it is the case, you can access your alfresco repo through the proxy endpoint in share and all requests will have authentication ticket attached to them before getting forwarded to your repo !

In other terms, use this endpoint :

/share/proxy/alfresco/api/upload

Instead of :

/alfresco/service/api/upload

and You won't even have to attach a ticket to your requests.

Upvotes: 3

mitpatoliya
mitpatoliya

Reputation: 2037

You need to follow these steps to achieve what you are looking for.

1) Reading File:

To display content of PDF file already uploaded you need to read content of file. You are able to do it successfully using following API call.

http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/21384098-19dc-4d3f-bcc1-9fdc647c05dc/latexexemplo.pdf

2) Capture New Content:

Capture new file content from User from applet. I guess you are storing it in some String variable.

3) Edit Existing File Content:

Issue here is that you cannot simply edit any pdf file using any of out of box Alfresco REST API (as far as I know). So you need to create your own RESTFul API which could edit pdf file's content. You can consider using some third party libraries to do this job. You need to plugin logic of editing pdf in RESTFul API

4) Changes back to Repo: Call Your API from Step 3:

You could also have look at this plugins which could fulfill your requirements.

https://addons.alfresco.com/addons/alfresco-pdf-toolkit

Hope this helps.

Upvotes: 1

Related Questions