setBinaryData

Posted by Rollbase User on 08-Feb-2012 12:37

Hello, I am trying to use setBinaryData(REST) in java. When I looked at the sample code in chapter 15, it was used by ByteArr which I am sure about. Was the sample code in Java? Could you give me more details about the setBinaryData? Thanks.

All Replies

Posted by Admin on 08-Feb-2012 12:50

There are two setBinaryData methods: SOAP and REST.



SOAP has example written in Java. REST has no example. It's essentially identical to setDataField API.

Posted by Admin on 08-Feb-2012 12:57

Could you explain about ByteArr?

I was not able to find class ByteArr in Java



in the question, i meant that I am not sure about ByteArr.

Posted by Admin on 08-Feb-2012 13:26

You should use Rollbase WSDL. Typically SOAP framework creates Java stubs from WSDL which can be compiled and used by client code.



If you're new to SOAP you may find it easier to use REST - this way you'll need only HTTP client.

Posted by Admin on 08-Feb-2012 13:49

Sorry, Pavel.

I am not familiar with Byte Array.



Can you take a look at my code and see what I am not doing wrong.



File new_file = new File(move_dir+filename);

InputStream ins = new FileInputStream(new_file);

ByteArrayOutputStream baos = new ByteArrayOutputStream();



try {

int read=0;

byte[] data = new byte[(int)new_file.length()];

while ( (read = ins.read(data)) != -1 ) {

baos.write(data, 0, read);

}

} catch (IOException ex) {}



String post_url="http://localhost:8080/rest/api/setBinaryData";

HttpClient httpclient=new HttpClient();

PostMethod pmethod=new PostMethod(post_url);



pmethod.addParameter("sessionId",session_id);

pmethod.addParameter("id",id);

pmethod.addParameter("fieldName","pdf_document");

pmethod.addParameter("contentType","application/pdf");

pmethod.addParameter("fileName",filename);

pmethod.addParameter("binData",baos.toByteArray().toString());

httpclient.executeMethod(pmethod);



Thanks,



Jae

Posted by Admin on 08-Feb-2012 15:35

I don't see Base64 encoding. Consider this modification which uses org.apache.commons.codec.binary.Base64



File new_file = new File(move_dir+filename);



int n = (int)new_file.length();

byte[] arr = new byte[n];



InputStream ins = new FileInputStream(new_file);

ins.read(arr, 0, n);

ins.close();



String post_url="http://localhost:8080/rest/api/setBinaryData";

HttpClient httpclient=new HttpClient();

PostMethod pmethod=new PostMethod(post_url);



pmethod.addParameter("sessionId",session_id);

pmethod.addParameter("id",id);

pmethod.addParameter("fieldName","pdf_document");

pmethod.addParameter("contentType","application/pdf");

pmethod.addParameter("fileName",filename);

pmethod.addParameter("binData", Base64.encodeBase64String(arr));

httpclient.executeMethod(pmethod);



Posted by Admin on 08-Feb-2012 15:36

You can do the same in PHP using CURL package.

Posted by Admin on 08-Feb-2012 17:27

Hello Pavel,



I tried your code and it populate the record without any error, but when I see the uploaded file, it is 0 sized file.

It seems it does not uploaded properly.

is there another way to convert a file to binary array?



Thanks.

Posted by Admin on 14-Feb-2012 03:23

I had the same problem. How I fixed it was to use urlencode with PHP. Here's my full code which works:



This code downloads an attachment record from rollbase, then merges them and creates a new attachment record and uploads the merged pdf attachment.







/*

* Performs a REST call (POST or GET)

*/

function REST($type,$url,$action,$params)

{

if ($type === "POST")

{

$session = curl_init($url);

curl_setopt($session, CURLOPT_POST, true);

curl_setopt($session, CURLOPT_POSTFIELDS, $params);

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($session);

curl_close($session);



if ($action === "create")

{

$respXML = new SimpleXMLElement($response);

$createdID = xml_attribute($respXML, 'id');

return $createdID;

}

else if ($action === "setBinaryData" || $action === "setDataField")

{

print_r($response);

$pos = strrpos($response, "has been cleared on");

if ($pos ===

Posted by Admin on 14-Feb-2012 03:28

Take note of the urlencode function plus the base64 encode function: (PHP)



$encodedPdf = urlencode(chunk_split(base64_encode($pdfBinary)));

Posted by Admin on 14-Feb-2012 10:01

Thanks for sharing your code. It is important to use correct parameter name "value" for encoded data: it was "binData" in original post.

This thread is closed