You need to sign in to do that
Don't have an account?
How to store an Attachment with a JPEG file from HttpRequest.
Hi All,
I want to store a JPEG file in an Attachment object. The source of this JPEG file is an HttpRequest Object. Here's my code:
Http http = new Http();
HttpRequest httpRequest = new HttpRequest();
httpRequest.setEndPoint('http://profile.ak.fbcdn.net/v225/1757/61/q785659914_8737.jpg'); //jpeg file to retrieve!
httpRequest.setMethod('GET');
httpRequest.setHeader('Content-Type', 'image/jpeg');
string responseBody = http.send(httpRequest).getBody();
//system.debug(responseBody);
Attachment a = new Attachment();
Blob b = Blob.valueOf(responseBody);
a.body = b;
a.ParentId = '0038000000l4H5hAAE'; //a ContactId
a.contentType = 'image/jpeg';
a.name = 'Hildegunn';
insert a;
First of all, I create an HTTPRequest object pointinf to the URL of the JPEG file I want to grab, set method (GET) and content type. Then I submit the request and store body's response (THE JPEG FILE!). If I debug the responseBody variable, I get the ASCII content of the image file. So, the file is correctly retrieved.
The next part of the code, I create an Attachment object, link with corresponding ParentId, create Blob from responseBody and set Attachment's body.
Everything looks ok, I go to Contact's attachments and see the brand new attachment, BUT when i click VIEW attachment link, i get the following response:
https://files.na6.content.force.com/servlet/servlet.FileDownload?oid=00D80000000cEyD&otk=DrIT7n0CrBzY0Aw.rwJj61MiYURrs732h1pr1rj4l3FbqKulnu0pr5xq1.yoY_04&retURL=%2F0038000000l4H5hAAE&rtype=D
Looking the response in more detail, I have the following HTML:
<html>
<body>
<img src="https://files.na6.content.force.com/servlet/servlet.FileDownload?oid=00D80000000cEyD&otk=DrIT7n0CrBzY0Aw.rwJj61MiYURrs732h1pr1rj4l3FbqKulnu0pr5xq1.yoY_04&retURL=%2F0038000000l4H5hAAE&rtype=D"
alt="https://files.na6.content.force.com/servlet/servlet.FileDownload?oid=00D80000000cEyD&otk=DrIT7n0CrBzY0Aw.rwJj61MiYURrs732h1pr1rj4l3FbqKulnu0pr5xq1.yoY_04&retURL=%2F0038000000l4H5hAAE&rtype=D"/> </body>
</html>
As you can see, the img tag is not able to retrieve the file, so is displaying the ALT attribute.
Any ideas?
Regards,
Aldo
I found a link that knows how to do this...
http://stackoverflow.com/questions/9632570/upload-image-from-android-client-to-salesforce-custom-object
In the recent Spring release (v24) ... you can now work with binary data (blobs in apex) directly in the http request or response. Here's an example of making a HTTP GET request for an image PNG file, and saving it to the document object in salesforce.
Here is the code in that link:
HttpRequest r = new HttpRequest();
r.setMethod('GET');
r.setEndpoint('http://www.pocketsoap.com/osx/soqlx/soqlxicon.png');
Http http = new Http();
HttpResponse res = http.send(r);
blob image = res.getBodyAsBlob();
Document d = new Document();
d.name = 'logo.png';
d.body = image;
d.folderId = UserInfo.getUserId();
insert d;
system.debug(d.id);