You need to sign in to do that
Don't have an account?
Dman100
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" });
{
// 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;
}
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();
}
{
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[]
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.
The key is that by the time you get your hands on the body, it's not base64 anymore, but a byte array.
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?