You need to sign in to do that
Don't have an account?
edemdev
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
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
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.
Thanks for providing the solution to the problem.
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.
I just realized I am missing some required fields.
I'll post back after I give that a shot.