Public class convertImageURLToFile {
|
public static Blob downloadImage(String imageUrl) {
|
Http http = new Http();
|
HttpRequest request = new HttpRequest();
|
request.setEndpoint(imageUrl);
|
request.setMethod('GET');
|
|
HttpResponse response = http.send(request);
|
|
if (response.getStatusCode() == 200) {
|
return response.getBodyAsBlob();
|
} else {
|
System.debug('Unable to download image. Status code: ' + response.getStatusCode());
|
return null;
|
}
|
}
|
|
|
public static String saveImageToAttachment(Blob imageBlob, String fileName) {
|
Attachment attachment = new Attachment();
|
attachment.Name = fileName;
|
attachment.Body = imageBlob;
|
attachment.ParentId = 'a3TC500000003dqMAA'; // 将文件关联到特定记录
|
insert attachment;
|
|
return '/sfc/servlet.shepherd/version/download/' + attachment.Id;
|
}
|
}
|