• Aldof
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

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&amp;otk=DrIT7n0CrBzY0Aw.rwJj61MiYURrs732h1pr1rj4l3FbqKulnu0pr5xq1.yoY_04&amp;retURL=%2F0038000000l4H5hAAE&amp;rtype=D"

alt="https://files.na6.content.force.com/servlet/servlet.FileDownload?oid=00D80000000cEyD&amp;otk=DrIT7n0CrBzY0Aw.rwJj61MiYURrs732h1pr1rj4l3FbqKulnu0pr5xq1.yoY_04&amp;retURL=%2F0038000000l4H5hAAE&amp;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 


 

Message Edited by Aldof on 10-06-2009 10:20 AM
  • October 06, 2009
  • Like
  • 0