Thursday, 24 October 2013

Sending bytearray and other parameters to the webscript at the same time

Hey everyone !

There may be the scenarios where you want to upload a file to the alfresco repository through the webscript and also want to pass some parameters to the webscript.
That means you want to send the uploaded file (bytearray) alongwith some parameters to the webscript.

In this scenario, You definitely will think of writing a POST webscript. Absolutely fine.

However, It is not possible to send both the content (other parameters) and uploaded file to the webscript at the same time. You may try doing it from POSTER plugin, it will not allow.
Note : Poster is a firebug plugin I generally use for testing POST webscripts.

In order to send bytearray of the content and also pass other parameters to the webscript. Here is the trick.

You will need to do below -
1. Open poster
2. Provide your webscript URL.
    e.g. http://localhost:8080/alfresco/service/test/upload
3. Provide the parameters you want to send as the querystring. Hence, modify the URL as below.
    e.g. http://localhost:8080/alfresco/service/test/upload?nodeid=parentid&type=thumb
4. In poster, click on Content to send tab. Open the file you want to upload and Click on POST to hit the webscript.

In your controller class of the webscript -
You can retrieve the uploaded content as mentioned below.
1. For parameters, use request.getParameter("nodeid");  This we all have used.
2. To retrieve uploaded content, use request.getContent() and this way you will get the uploaded content. You can get the inputstream of the content and use it for further processing.

This way you can pass bytearray and other parameters to the webscript at the same time.

Hope this will help you at some point of your coding experience in alfresco.


No comments:

Post a Comment