+ Start a Discussion
jaxdmasterjaxdmaster 

store external image as an attachment

Hi Guys,

 

I've a url of external image and I want to download it and store it to attachment via apex code itself. Below code I am writing

 

   Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://t0.gstatic.com/images?q=tbn:ANd9GcRbKQv8h5YamhGNCkc5GZg58n2y-rG8Ju7jUGpS_L4CUH6VI7e-gg');
       req.setMethod('GET');
       HttpResponse res = h.send(req);


        
		Attachment attach = new Attachment();
               Blob b=Blob.valueof(res.getBody());

		attach.Body = b;
		attach.Name = String.valueOf('FirstPageImage4.png');
		attach.ContentType = 'image/png';
		attach.ParentId ='a00E0000001Ayk0'; 
		insert attach;

 I've also added URL to remote host. But when run it. Attachment stores well but when i view it screen comes blank. I saw the body field it show something like '7+777+77.....'

 

But when samething i do manually (Downloaded image to desktop and then uploaded it to attachment on particular record)

It works fine.

 

What I am missing?

 

Thanks