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
pawanpawan 

Inserting attachments in c#

I've been trying to insert properly encoded (base-64) data for attachments in C# but it's notworking.  In fact it is the same data that salesforce is passing me.  I am passing it as a string but I get "Value ... not of required type base64 on field body", so does that mean I have to pass it as a type other than a string? 

mobileTestmobileTest

HI pawan,

It is soooo easy to do attachments in .Net because it does the base64 encoding for you (practically).  The code below is all you need to do to create the attachment part of the message:

//First get a reference to the file you want to send
System.IO.FileInfo attachment = new System.IO.FileInfo((string)fileName);

//Open the file for binary read operation
System.IO.FileStream fs = attachment.OpenRead();

//create a variable to hold the binary (byte) data
byte[] bytes = new byte[fs.Length];

//read the file into the byte array in it's entirety
fs.Read(bytes, 0, (int)fs.Length);

//close the file
fs.Close();

//create the "record" to insert into the database
sforceHelper.sForce.mapEntry[] record = new sforceHelper.sForce.mapEntry[4];
record[0] = new sforceHelper.sForce.mapEntry();
record[0].key = "body";  //set the body value
record[0].value = bytes; //to the byte array (binary file data).

As you can see, there is no Convert.ToBase64 blah blah blah in this code.

If you still have problems after using this implementation, please include your SOAP request in the post (you can remove most of the base64 data for brevity).

 

pawanpawan

I have a attachment body which is a base64 string and I want to insert it in to salesforce.  However I am getting fault code 1212 when I try to convert it.  I have also tried the method you described above and that also gives me tohe same error.

record[k] = new sForceConn.mapEntry();
record[k].key = "body";
record[k++].
value = Convert.FromBase64String(attachment.body);

DevAngel2DevAngel2

Hi Pawan,

If the attachment is an email attachment, it may already be in base64 depending on the mail system.  What you want to send is a byte array.  This will properly be serialized by the soap client as base64binary.  Converting to a base64 string results in a string and is serialized as such.

 

pawanpawan

I've tried passing a byte array but I get the following exception:

Value [82, 73, 70, 70, 106, 66, 1, 0, 65, 86, 73, ... 0, 0, 0, 0, 0] not of required type base64 on field body

attachment.body is already a base64 string so when I call Convert.FromBase64String I am converting it to a byte array.

record[k] = new sForceConn.mapEntry();
record[k].key = "body";
record[k++].
value = Convert.FromBase64String(attachment.body);

What am I doing wrong?

-Pawan

DevAngel2DevAngel2

Hi Pawan,

Typically, the base64 part of the soap message will look like this:

    <tns:mapEntry id="id4" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">body</key>
      <value xsi:type="xsd:base64Binary">IA0KKysrKysrKysrKysrKysrICBCRUdJTiBTT0FQIFRSQU5TQUNUSU9OICArKysrKysrKysrKysr
KysNCioqIEJFR0lOIFNPQVAgUkVRVUVTVDogNS85LzIwMDMgOTo1Njo1MiBBTQ0KVVJMOiBodHRw
Oi8vbmExLnNhbGVzZm9yY2UuY29tL3NlcnZsZXQvc2VydmxldC5Tb2FwQXBpDQogDQo8P3htbCB2
ZXJzaW9uPSIxLjAiIGVuY29kaW5nPSJ1dGYtOCI/Pg0KPHNvYXA6RW52ZWxvcGUgeG1sbnM6c29h
cD0iaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvc29hcC9lbnZlbG9wZS8iIHhtbG5zOnNvYXBl
bmM9Imh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3NvYXAvZW5jb2RpbmcvIiB4bWxuczp0bnM9

I looks like you are going from this to the ascii or unicode representation of the characters.  Start with a file that you read binary and get that to work.  Then, assuming you get that to work, make sure you know with absolute certainty the data representation of the attachment. 

How are you getting the attachement?  Is it from an email message?  If so, are you using MAPI, Redemption or CDO?  I've done the whole email attachment thing before and if you give some more information I may be able to spot the issue.

 

pawanpawan

I think my bse64 string is correct but in my  SOAP request it says <value xsi:type="xsd:string"> and not <value xsi:type="xsd:base64Binary"> like yours.  I think this is the problem.  How do I make it base64Binary?

<tns:mapEntry id="id5" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">body</key>
      <value xsi:type="xsd:string">UklGRmpCAQBBVkkgTElTVIwFAABoZHJsYXZpaDgA

DevAngelDevAngel

Hi Pawan,

The .Net SoapSerialize will serialize an array of bytes as base64binary.  You need to have the base64 attachment in a byte array, not converted to a string. 

pawanpawan

When I have the base64 attachment as a byte array my request is incorrect: (I get this when I try the method suggested by mobileTest, or if I convert my base64 string to a byte array)

    <tns:mapEntry id="id5" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">body</key>
      <value href="#id6" />
    </tns:mapEntry>
    <soapenc:Array id="id6" soapenc:arrayType="xsd:unsignedByte[82944]">
      <Item>82</Item>
      <Item>73</Item>
      <Item>70</Item>
      <Item>70</Item>
      <Item>106</Item>
      <Item>66</Item>
      <Item>1</Item>
      <Item>0</Item>
      <Item>65</Item>

Basically, I have a base64 string and I want to upload the attachment....

pawanpawan
How do I fix this?
DevAngelDevAngel

Pawan,

I can't tell you how to fix it.  I don't know what the format is of the attachment that you are trying to send.  I previously asked you how you were obtaining your attachment.  The task seems to be one of type conversion, either from a base64 string to a byte array or some other combination.

I need to know more about what the insies and outsies are before I can take you any farther.  It really is a simple task to do this.

pawanpawan

Basically, I am given the base64 string and I want to add an attachment using this.  For testing purposes I am using a base64 string returned by the query for the body of a salesforce attachment.  This returns a string such as UklGRmpCAQBBVkkgTElTVIwFAABoZHJsYXZpaDgA....

Then I am trying to create a new attachment with the same "body", so I do:

record[k] = new sForceConn.mapEntry();
record[k].key = "body";
record[k++].
value = Convert.FromBase64String(attachment.body);

where attachment.body is the base64 string shown above.
Convert.FromBase64String(attachment.body); -> returns a byte []

But in my SOAP request it looks like instead of serializing it to a base64 string.

<tns:mapEntry id="id5" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">body</key>
      <value href="#id6" />
    </tns:mapEntry>
    <soapenc:Array id="id6" soapenc:arrayType="xsd:unsignedByte[82944]">
      <Item>82</Item>
      <Item>73</Item>
      <Item>70</Item>
      <Item>70</Item>
      <Item>106</Item>
      <Item>66</Item>
      <Item>1</Item>
      <Item>0</Item>
      <Item>65</Item>

If you need any more information please let me know.  Thanks for putting up with me

-Pawan

mceruttimcerutti

Did anyone ever get this to work.  I used the sample from mobileTest, but the data gets encoded as an array instead of base64.  It has been awhile since there was activity on this thread.  I am hoping a resolution was found and not posted.

Thanks

 

adamgadamg

Was the tech note here http://www.sforce.com/us/resources/tn-6.jsp able to help?

Adam