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
nelrib88nelrib88 

Downloading Documents

im trying to download a specific document that is selected by a user. im using the code below, the url being sent is https://na7.salesforce.com/servlet/servlet.FileDownload?file=D015A0000005QphPIAS. the issue im facing is that the saved file always contains the body of the salesforce login page (the returned file code is below).

 

HttpClient httpclient = new HttpClient();
                GetMethod get = new GetMethod(salesForceFacade.getSfSession().getInstanceUrl() + "/servlet/servlet.FileDownload?file="+salesforceDocument.getId());

                // set the token in the header
                get.setRequestHeader("Authorization", "OAuth " + salesForceFacade.getSfSession().getAccessToken());
                httpclient.executeMethod(get);
                System.out.println(get.getURI());
                System.out.println(get.getDoAuthentication());
                System.out.println(get.getResponseBodyAsString());
               
               // URL url = new URL(salesForceFacade.getSfSession().getInstanceUrl()+"/servlet/servlet.FileDownload?file%3D"+salesforceDocument.getId());
                    InputStream is = get.getResponseBodyAsStream();
                    OutputStream os = new FileOutputStream("C:\\Users\\user\\Desktop\\"+salesforceDocument.getName()+"."+salesforceDocument.getType());

                    byte[] b = new byte[1024];
                    int length;

                    while ((length = is.read(b)) != -1) {
                            os.write(b, 0, length);
                    }

                    is.close();
                    os.close();

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">





<script>
var escapedHash = '';
var url = 'https://login.salesforce.com/?ec=302&startURL=%2Fservlet%2Fservlet.FileDownload%3Ffile%3D015A0000001QphPIAS';
if (window.location.hash) {
   escapedHash = '%23' + window.location.hash.slice(1);
}
if (window.location.replace){ 
window.location.replace(url + escapedHash);
} else {;
window.location.href = url + escapedHash;
} 
</script>

</head>


</html>



<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

The REST Api has always returned a URL for the binary fields (the SOAP API does not). simply do a GET on the URL to get the data.

All Answers

SuperfellSuperfell

The FileDownload servlet is not an API, don't expect it to behave like one.

 

Either use the SOAP api and query the Body field of the document, or use the rest api to fetch the body resource (/services/data/v21.0/document/{id}/body)

nelrib88nelrib88

i was able to query the body and get a base64 string that i then decoded to get the file but ever since last week that no longer works and the body returned by the api is a url (/services/data/v20.0/sobjects/Document/{id}/Body). can you help me in figuring out what i have to do with the url to get the file.

SuperfellSuperfell

The REST Api has always returned a URL for the binary fields (the SOAP API does not). simply do a GET on the URL to get the data.

This was selected as the best answer
nelrib88nelrib88

i have changed the code and now when doing a get on https://na11.salesforce.com/services/data/v20.0/Document/{id}/body  i get {"message":"The requested resource does not exist","errorCode":"NOT_FOUND"} in response.

SuperfellSuperfell

IIRC, its /services/data/v20.0/sobjects/Document/{id}/Body, check the docs.

nelrib88nelrib88

Alright thanks so much i didn't realize i had that url wrong.

RajKuchRajKuch

I am looking into requirement in saleforce.com to download or move a pdf file from their server and  .. I have the URL and can access the URL ..but don't know which Java API in saleforce provides for java development to get move this file from saleforce to another server.  I would appreciate any response.  I saw some replies by Simon on similar downloading files but I am not able to use it.

thanks

raj