Lacking experience...REST Attachment

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

Lacking experience...REST Attachment

#1

Postby alexmcclune » Thu Jun 16, 2016 3:45 pm

Hey,

This isn't ebase related directly....just something I am hoping to achieve with Ebase.

Essentially I am using release 5.1 and the REST resources to successfully create/lookup records in Salesforce. A new piece of functionality I want to create is the ability to insert an Attachment.

I am currently using the Ebase functionality to allow users to upload attachments, these end up on the ebase server just now...what I am struggling with is how to convert these to "something" that I could then POST back to Salesforce as an Attachment record (Blob, base64 etc).

Can anyone explain how I could achieve this in Ebase? Apologies...the first time in doing something is always the hardest.
0 x

Steve
Moderator
Moderator
Posts: 419
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#2

Postby Steve » Thu Jun 16, 2016 4:21 pm

Hi Alex,

I am not really familiar with Salesforce. but reading on the forum you could attach these as a Content-Type: multipart/form-data;

https://developer.salesforce.com/docs/a ... e_blob.htm

The REST API does not support multipart form data, so you will probably have to build this yourself:

https://hc.apache.org/httpcomponents-cl ... mPost.java

Either that, or you could maybe base64encode the file and add as a parameter to the request? I am not sure what would happen to it on SalesForce though.


I am not sure if this information helps...

Steve
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#3

Postby alexmcclune » Thu Jun 16, 2016 4:30 pm

Thanks Steve,

I've been reading a lot of that today. I was hoping you had a sample/example of converting the file from the server to blob or encoded etc?

I have tried a few things, mostly client side without success, still googling though! Worth noting my REST API can create the "Attachment" record as I would expect...I am just missing how to convert the file to the right format to post.
0 x

Steve
Moderator
Moderator
Posts: 419
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#4

Postby Steve » Fri Jun 17, 2016 9:34 am

Hi Alex...

I wrote a function to Base64Encode a file:

Code: Select all

importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
importPackage(java.io);
importClass(org.apache.commons.codec.binary.Base64);

var filename = "C:/Temp/testPic.png";

controls.TXT.text.text = encodeFileToBase64Binary(filename);

function encodeFileToBase64Binary(filename){
    var encodedfile = null;
    try {
    		var file = new File(filename);
        var fileInputStreamReader = new FileInputStream(file);
        var buffer = new Packages.java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, file.length());
        fileInputStreamReader.read(buffer);
        encodedfile = Base64.encodeBase64String(buffer);
    }
    catch (e) 
    {
    	log(e);
    }
     
    return encodedfile;
}
Note that you will need the Apache commons IO jar file in your WEB-INF/lib folder of the Ebase server. If you are using version 5.1 of Ebase, then you should have this already.

Also note that javaScript does not cope with byte[] very well, so I have used Java reflection to create a byte array. See the line that creates buffer.

I hope this helps

Steve
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#5

Postby alexmcclune » Fri Jun 17, 2016 11:48 am

Almost perfect! Very little tweaks required but that's what I had hoped you had :D Really appreciate it.
0 x


Who is online

Users browsing this forum: No registered users and 14 guests