+ Start a Discussion

Attachments - Converting to Base64 in C#

I'm in the process of writing a recruiting portal which sits on top of the 'Recruiting Manager' application.  I would like to allow  users to upload a resume, and to that end have included a  'File' HTML input, which allows users to post a file to my code-behind page.  I would like to add this file as an Attachment to the 'Candidate' object.

I am able to create the Attachment record, and relate it to the Candidate object.  However, when I then try to download the file from SalesForce, it comes back as junk data.  I have isolated this to the need to convert the body from the byte[] returned by the HTML Input to a base64 byte[], for use by the Web Service API.

However, the only example I can find of how to do this on the boards, or in any SalesForce documentation, uses Visual Basic, and shows the developer using the 'Redim' method to resize the elements in the byte[] to Int64 before populating the array.

As I am developing in C#, I was wondering if anyone has a suggestion on how to do something similar in C#.  I have seen a few posts recommending downloading and installing custom components, but I am hosting my code on an external box, on which I have no control to add components.  I have tried using various methods for the Convert class, but none seem to be working.

Any ideas?
If you're using the enterprise WSDL, then .NET will do this automatically for you, you give it an array of bytes, it will automatically base64 encode it to send it over the wire. If you're using the partner WSDL, then you can use Convert.ToBase64String(buff) (where buff is a byte []).

Thank you for the prompt response.  I am using the enterprise WSDL, and when I tried just passing it a byte[] yesterday, the file I recieved from SalesForce was junk.

I tried it again today, and it worked without issue.  I don't know why I was having trouble with it yesterday.  Must have been corruption from the wire or something.


Please view this code it may helps you to attache a file. You can attach any type of file. I used the pdf order for my customers. this is C# Dot Net.. you can use the Java as well. but syntax would be changed.

-Mahendra Singh - PMP
Infogain India Pvt Ltd.
CRM & CTI Software Projects.
Attachement using C#... Dot

protected void attachpdfPDF(object sender, EventArgs e)
            string path = Server.MapPath("~/");

            string fileName = "New" + DateTime.Now.Ticks + ".pdf";
            string attachmentname=fileName;
            GeneratePDF(path, fileName, false, "");
            try {
                if (File.Exists(path + fileName))
                    fileName =path + fileName;
                FileStream fs =     File.OpenRead(fileName);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, Convert.ToInt32(fs.Length));

                sforce.Attachment attach = new Attachment();
                attach.Body = bytes;
                attach.Name = attachmentname;
                attach.IsPrivate = false;
                attach.ParentId = "12345XXXXXXXXXX";

                //SaveResult sr = binding.create(new com.sforce.soap.enterprise.sobject.SObject[] {attach})[0];
                QueryResult qr = null;
                binding.QueryOptionsValue = new QueryOptions();
                binding.QueryOptionsValue.batchSize = 250;
                binding.QueryOptionsValue.batchSizeSpecified = true;
                SaveResult[] oResults = binding.create(new Attachment[] { attach });
            } catch (FileNotFoundException fnf) {
                Response.Write("File Not Found: " +fnf.Message.ToString());
            } catch (IOException io) {
                Response.Write("IO: " + io.Message.ToString());            

lucysome luclucysome luc
To decode a Base64 string, simply use Convert.FromBase64String(). E.g.L

var base64 = "YWJjZGVmPT0=";
var data = Convert.FromBase64String(base64);

At this point, data will be a byte[] (not a string). If we know that the byte array represents a string in UTF8, then it can be converted back to the string form using:

var str = Encoding.UTF8.GetString(data);

This will output the original input string - abcdef== in this case.
more on qr code for byte array using c#.net (http://www.keepdynamic.com/dotnet-barcode/barcode/qr-code.shtml)