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
Dman100Dman100 

Decode Base64 String

I'm using the retrieve call thru the API to return a pdf file stored in the Document object.
 
I have successfully retrieved the fields from the Document object and displayed them on the page.  But, I am not able to decode the body field base64 string and display the content on the page?
 
This is what I have:
 
private void retrieve()
    {
        // Invoke retrieve call and save results in an array of SObjects
        sObject[] records = sfdc.retrieve("Name, Type, Description, ContentType, BodyLength, Body", "Document", new String[] { "01570000000GXCZ" });
        // Iterate through the results
        for (int i = 0; i < records.Length; i++)
        {
            Document document = (Document)records[i];
            // Get the document properties
            Label1.Text = "Name is: " + document.Name + "<br />" +
                "Type is: " + document.Type + "<br />" +
                "Description is: " + document.Description + "<br />" +
                "ContentType is: " + document.ContentType + "<br />" +
                "BodyLength is: " + document.BodyLength + "<br />" +
                "Body is: " + document.Body;
        }
    }
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        login();
        retrieve();
    }
 
When I run the page, this is what displays:
 
Name is: Odyssey System Requirements November 2007 Full
Type is: pdf
Description is: Supported Odyssey System Requirements updated for November 2007
ContentType is: application/pdf
BodyLength is: 116243
Body is: System.Byte[]
 
I've tried: Convert.FromBase64String(bodystring);, but that doesn't work.
 
Anyone know how I can decode and display the content for the Base64 string stored in the body field of the Docment object?
 
Thanks.
AcronymAcronym
I believe .NET deserializes the base64 into the byte array.  This means all you need to do is stream the byte array to the file system in a temp folder, then launch the document.  The file associations should take care of finding the right app to launch to display the document.

The key is that by the time you get your hands on the body, it's not base64 anymore, but a byte array.
Dman100Dman100

Thanks Acronym,

I was trying to create the pdf on my file system just as a test, but the byte array isn't getting written into the PDF...I haven't worked with FileStream very much, so I'm kind shooting blind here.

This is what I tried:

private void retrieve()
    {
        // Invoke retrieve call and save results in an array of SObjects
        sObject[] records = sfdc.retrieve("Name, Type, Description, ContentType, BodyLength, Body", "Document", new String[] { "01570000000GXCZ" });

        // Iterate through the results
        for (int i = 0; i < records.Length; i++)
        {
            Document document = (Document)records[i];
            // Get the document properties
            FileStream file = new FileStream("f:\\test.pdf", FileMode.Create);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(document.Body);
            sw.Close();
        }

    }

 

The PDF gets created, but errors when opening saying it wasn't decoded correctly or damaged?

Dman100Dman100
I got if figured out...I needed to use BinaryWriter instead of StreamWriter.