Sending attachment through Rest API call to Rollbase

Posted by anu31221@gmail.com on 24-Apr-2014 21:25

Not able to send attachment through Rest API call to Rollbase. Any one can share working code to send attachment

Here is the code



string mypath = Path.Combine(Path.GetTempPath(), attachment.DisplayName); //Store email attachment file
                                attachment.SaveAsFile(mypath);
                                FileInfo file = new FileInfo(attachment.DisplayName);
                                string contenttye = "Applicaion/" + file.Extension.Replace(".",string.Empty);
                                byte[] byteArray = System.IO.File.ReadAllBytes(mypath);
                                string urlattachmen = string.Format("{0}create2?sessionId={1}&objName=$R_DOCS&file={2}&R2465433={3}&output=json", API_Method.Utils.GlobalURL, Globals.ThisAddIn.SessionID,mypath, o["id"].ToString());
                                System.Net.WebRequest reqs = System.Net.WebRequest.Create(urlattachmen);
                                reqs.ContentType="Content-Type,"+ "application/x-www-form-urlencoded; charset=UTF-8";
                                reqs.Method = "Get";
                                reqs.ContentLength = byteArray.Length;
                                //true means no proxy
                                System.Net.WebResponse resps = reqs.GetResponse();
                                System.IO.StreamReader srs = new System.IO.StreamReader(resps.GetResponseStream());
                                string result = srs.ReadToEnd().Trim();
                                File.Delete(mypath); 

Posted by sedwin on 25-Apr-2014 07:27

Hello Anu,

If you are trying to attach a document for the created record, then you can use REST API setBinaryData by passing the base-64 encoded binary value of the file. This will attach the file to 'file upload' field.

Hope this helps.

Thanks,

Sopna.

All Replies

Posted by Bill Wood on 25-Apr-2014 06:39

First a disclaimer that I have not done this myself, but looking at the doc in Ch. 15 of Rollbase in Action....

  • create2 (or update2) don't seem to allow the content-type on attachments.
  • setDataField does support setting the value of a File Upload field. The doc for this in REST API section may give you a hint on how to go forward.

Is your use case one where you want the File Upload field? ( the code snippet you provided looked like you might be trying something else )

Posted by anu31221@gmail.com on 25-Apr-2014 06:56

I am trying to send the attachment from outside c# (DOTNET) class.
First I have created the record, then trying to attach a document for that record.
We have tried to follow RES API document, but still not working.



[collapse]
On Fri, Apr 25, 2014 at 7:40 AM, Bill Wood <bounce-wood@community.progress.com> wrote:
Reply by Bill Wood
First a disclaimer that I have not done this myself, but looking at the doc in Ch. 15 of Rollbase in Action....

- create2 (or update2) don't seem to allow the content-type on attachments.
- setDataDef does support setting the value of a File Upload field. The doc for this in REST API section may give you a hint on how to go forward.

Is your use case one where you want the File Upload field? ( the code snippet you provided looked like you might be trying something else )
Stop receiving emails on this subject.

Flag this post as spam/abuse.


[/collapse]

Posted by sedwin on 25-Apr-2014 07:27

Hello Anu,

If you are trying to attach a document for the created record, then you can use REST API setBinaryData by passing the base-64 encoded binary value of the file. This will attach the file to 'file upload' field.

Hope this helps.

Thanks,

Sopna.

Posted by Bill Wood on 25-Apr-2014 07:31

The language you are writing the client in does not particularly matter - what matters is which API you are using.   For external clients, the APIs can be SOAP, or REST (in particular, HTTP(s)/JSON and HTTP(s)/XML. ). From your code it seems you are using the JSON REST. 

My other point is that an "attachment" to an object in Rollbase is described as a File Upload in the UI (because if a user added a document to an object in the UI, it is done via a File Upload field.  

My reading of Chapter 15 (approximately on p.55) says that you should try the setDataField method. 

I dont know if you already tried it, but this provides a way to upload a base64 encoded byte stream as a named File attachment with Content-type to an object.   The"create2" method in your source snippet does not allow setting file name and content-type on individual object fields.  

Posted by anu31221@gmail.com on 25-Apr-2014 09:44

Are asking to try with similar code as below. I am going to try tonight

string mypath = Path.Combine(Path.GetTempPath(), attachment.DisplayName); //Store email attachment file

                               attachment.SaveAsFile(mypath);

                               FileInfo file = new FileInfo(attachment.DisplayName);

                               string contenttye = "Applicaion/" + file.Extension.Replace(".",string.Empty);

                               byte[] byteArray = System.IO.File.ReadAllBytes(mypath);

// Convert the binary input into Base64 UUEncoded output.     string base64String;    

try {        base64String =           System.Convert.ToBase64String(binaryData,                                  0,                                 binaryData.Length);  

}    catch (System.ArgumentNullException) {                  System.Console.WriteLine("Binary data array is null.");  

    return;  

}

Call the Rollbase RestAPI

setBinaryData?sessionId={1}&id={2}&fieldname=FileUploadFieldName&value =binData &contentType="application/pdf"&fileName="invoice.pdf"

Posted by IramK on 03-Nov-2014 10:40

Hello,

I am trying to achieve the same with a .pptx file but I am unable to do so. Could you please suggest me on how do I set the binary data with a .pptx file? I have tried the following: community.progress.com/.../50278.aspx

Posted by pvorobie on 03-Nov-2014 11:13

Please remove spaces and quotes from your URL.

This thread is closed