Download file in Attachments in Salesforce

D
public class ex_file_inSF

{

public void getExcelReport()

{

String AccId = ApexPages.currentPage().getParameters().get('id');

Account acc = [Select Id from Account where Id =: AccId];

Http h = new Http();

HttpRequest req = new HttpRequest();

string url = 'URL_Path';

url = url.replace(' ', '%20');

req.setEndpoint(url);

req.setMethod('GET');

req.setHeader('Content-Type', 'application/xls');

// To download Pdf 'application/pdf'

// To download Document 'application/doc'

// To download img 'image/jpeg'

req.setCompressed(true);

req.setTimeout(120000);

HttpResponse res = null;

res = h.send(req);

/*string responseValue = '';

responseValue = res.getBody();

system.debug('Response Body for File: ' + responseValue); */

blob file = res.getBodyAsBlob();

Attachment n = new Attachment();

n.ParentId = AccId;                                //set where you want to Save/Attach file

n.Name = 'Excel_Report.xls';

n.Body = file;

n.contentType = 'application/xls';

// To download Pdf 'application/pdf'

// To download Document 'application/doc'

// To download img 'image/jpeg'

insert n;

}

}

About the author

kalpesh.surana
By kalpesh.surana

Category