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
edemdevedemdev 

Problem with Attachment retrieval

Hi,

Im trying to resolve a problem Im having with Attachments. I have no problem uploading them using the API after a conversionform .txt file to a Base64 byte array but when I try to retrieve them from the SF database using  "Save as..." through the website, the file which is retrieved is still in the base64 format.

This is the conversion code I am using, which I believe may be the cause of the problem

Sample code:

            FileStream fs = new FileStream(@fileName,FileMode.Open,FileAccess.Read);
            byte[] filebytes = new byte[fs.Length];
            fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
            string encodedData = Convert.ToBase64String(filebytes,Base64FormattingOptions.InsertLineBreaks);

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            filebytes = encoding.GetBytes(encodedData);

            //Create an attachment object to send to the license
            sforce.Attachment to_attach = new sforce.Attachment();

            //Set several properties
            to_attach.Name = fileName;
            to_attach.Body = filebytes;


I have also tried adding the MIME dat a to the string for conversion  like so:

string encodedData = si:type=\"xsd:base64Binary\">" +  Convert.ToBase64String(filebytes,Base64FormattingOptions.InsertLineBreaks);

but with no sucess????

Can anyone spot any errors or give som advice???

Thanks
edemdevedemdev
I managed to solve this. It seemed to be related to the Content-Type and it seems the conversion to Base64 isnt needed.

FileStream fs = new FileStream(@a_licName,FileMode.Open,FileAccess.Read);
 
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));

//Create an attachment object to send to the license
sforce.Attachment to_attach = new sforce.Attachment();

//Set several properties
to_attach.Name = a_licName;
to_attach.Body = filebytes;
to_attach.ContentType = "text";

I feel the API is rather unclear about the base64 conversion and it doenst seem to be needed at all.
RickyGRickyG
emourao -

Thanks for providing the solution to the problem.
SuperfellSuperfell
You're using the enterprise WSDL, and the base64 encoding/decoding is done for you automatically by .NET.
ndoshindoshi
I am running into problems when I call create on the binding and pass in the attachment.

Here is a sample of the code that I am running:

    Attachment attachment = new Attachment();
    SaveResult[] results;
    String attachmentId;
   
    logger.info("Creating attachment: " + fileName);
    logger.debug("Contents are: " + dataFile);
   
    attachment.setBody(dataFile.getBytes());
    attachment.setName(fileName);
   
    results = getSalesforceSessionManager().create(new Attachment[] {attachment});
    attachmentId = results[0].getId();



dataFile is a long String with newline characters that represents a text file
fileName is a String and represents the name of the file that I want to create

the attachmentId that I get back is null.
I think I need to put the files in a folder of some sort, but I am not sure how to do that.

ndoshindoshi
Actually, hold off on answering that.
I just realized I am missing some required fields.
I'll post back after I give that a shot.
ndoshindoshi
Works fine now.