function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
raviendiran p 9raviendiran p 9 

need to attach file to salesforce account using rest api in c#

Hi all,
I trying to upload attachment to salesforce account rest api using asp.net

We have followed the steps given in following link but couldn't succeed.
http://danlb.blogspot.in/2012/06/salesforce-rest-api-file-upload.html

please help me on this
NagaNaga (Salesforce Developers) 
Hi Raviendiran,

You can either base64 encode your binary data and include it when you post a new attachment to /sobjects/attachment or you can do a multipart request.  See the documentation for details: http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm

Best Regards
Naga Kiran
raviendiran p 9raviendiran p 9
Hi Naga,

Thanks for your response, but when i am trying to upload the document its getting upload. But i wanna to attach a file to an account.
If possible can you give some sample code.
dillip nayak 3dillip nayak 3
HI Ravi

I think you used salesforce webservice just use this code.If any problem let me know

                string ContactID = "0039000001Ku0uC";//just pass dynamic contactID here
                Attachment attachment = new Attachment();
                attachment.Body = System.IO.File.ReadAllBytes("File.csv");//just pass File here
                attachment.ParentId = ContactID;
                attachment.Name = "Test 1";
                sObject[] InsertAttcment= new sObject[] { attachment };
                api.Insert(InsertAttcment);


Thanks
Dillip
 
raviendiran p 9raviendiran p 9
hi dillip,

thanks for your respone. Actually i am using following code to upload document in salesforce and its working fine.

But i wanna to attach files under particular account in salesforce. 

pls suggest me on this.

public void upload()
        {
            var doc = new sfdcDocument();
            doc.Name = "TestDoct11jh4";
            doc.FolderId = "00l28000000dB3yAAE";
            doc.Type = "txt";
            string boundary = "----" + DateTime.Now.Ticks.ToString("x");
            
            var uri = instanceUrl + "/services/data/v24.0/sobjects/Document/";
            var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            req.Headers.Add("Authorization: OAuth " + accessToken);
            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.Method = "POST";
            var os = req.GetRequestStream();

         
            // Add header for JSON part
            string body = "";
            body += "\r\n--" + boundary + "\r\n"; ;
            body += "Content-Disposition: form-data; name='entity_document'\r\n";
            body += "Content-Type: application/json\r\n\r\n";

            // Add document object data in JSON
            var ser = new JavaScriptSerializer();
            body += ser.Serialize(doc);


            // Add header for binary part 
            body += "\r\n--" + boundary + "\r\n"; ;
            body += "Content-Disposition: form-data; name='Body'; filename='1.txt'\r\n";
            body += "Content-Type: binary/octet-stream\r\n\r\n";
        

            // Add header data to request
            byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
            os.Write(data, 0, data.Length);

            // Add file to reqeust
            FileStream fileStream = new FileStream(@"c:\temp\1.txt", FileMode.Open, FileAccess.ReadWrite);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                os.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();

            // Add trailer
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            os.Write(trailer, 0, trailer.Length);
            os.Close();


            // Do the post and get the response.
            WebResponse resp;

            try
            {
                resp = req.GetResponse();
            }
            catch (WebException ex)
            {
                resp = ex.Response;
            }

            //if (resp == null) return null;
            var sr = new System.IO.StreamReader(resp.GetResponseStream());
        }

public class sfdcDocument
        {
            public string Description { get; set; }
            public string Keywords { get; set; }
            public string FolderId { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
        }