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
VallabhaVallabha 

How to download attachments?

Hello All,
 
I looking for small help in regards to downloading attachments through SalesForce APIs.
 
I have few "Account" objects and some of these account objects have file attached with them. When I call "getUpdated' for "Account" object type, I get all the changed objects (for the given start and end time). However I am not able to retrieve the associated attachment.
 
I tried making use of the following call:
 

qr = binding.query("select Name, Body, ContentType from Attachment where ParentId='0018000000M34fSCCS");

Not sure if this is the correct way to download the file attachments. Does anyone knows what is the right way to download the file attachments using SalesForce API. Btw, I am using Partner WSDL for all my development.

Any help in this regard is greatly appreciated.

Thanks

-Vallabha

SuperfellSuperfell
How is it not working? does the query return no rows, does it return data but you don't know how to decode it?
VallabhaVallabha
Thanks Simon for your reply. The query is returning results.  I just want to know is that the only way to download the files or are there any other ways to get the attachments? Btw I am also looking for a way to decode the content I am receiving. Any help in these reagrds is appreciated.
 
Thanks again
-Vallabha
SuperfellSuperfell
This is the only programatic way to download attachments. You need to base64 decode the string supplied in the partner API results to get back to the array of bytes that is the attachment.
VallabhaVallabha
Yes I am getting the string value (containg attachment contents). Now if I decode using BASE64Decoder().decodeBuffer() and I get the actual attachment contents. However this is working fine for normal text files, however it fails for GIF files. Do I need to do anything else for non-text files?

My code looks like this:

            qr = binding.query("select Name, Body, ContentType from Attachment where ParentId='0018000000M34fSARC'");
             for(int i=0; i < qr.getSize(); i++){
                SObject acc = qr.getRecords(i);
                org.apache.axis.message.MessageElement[] fileds = acc.get_any();
                String fName = fileds[0].getValue();
                String fileContents = fileds[1].getValue();
                byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);
                fileContents = new String(bName);
                String body = fileds[2].getValue();
              
                // Lets try to write it to a file
                DataOutputStream out = new DataOutputStream(new FileOutputStream(System.getenv("TEMP")+ "\\" + fName));
                out.writeBytes(fileContents);
                out.close();
            }

Any help on how to handle GIF, .doc, other files would be of great help.

Thanks again
-Vallabha
SuperfellSuperfell
Why are you re converting the decoded byte array back to a string ?

String fileContents = fileds[1].getValue();
byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);
fileContents = new String(bName);


This will certainly be corrupting your data, you just need.

String fileContents = fileds[1].getValue();
byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);

OuputStream os = new FileOutputStream(System.getenv("TEMP")+ "\\" + fName);
os.write(bName);
os.Close();
chikpeachikpea

I downloaded some pdf attachments and tried to decode that attachment body using some online decoder, but it failed, got an error - Invalid length for a Base-64 char array.

 

Any suggestion???

Rick.BanisterRick.Banister
Relational Junction can do this, either to a database table or a file.