My problem is that I cannot view an image loaded from an external site into an img tag in my lightning component. The URL is with 'http' protocol not 'https', but when I see the Chrome Console, it tells that failed to load resource and I noticed that Salesforce try to go to the external site with the 'https' protocol and so it not find the resource. Why all this happens?
[SalesForce] External image not displaying in community
Related Solutions
You might be able to add something to the headers or the page to be able to access the actual photo url, but I'd avoid the whole thing & provide the photo in another form, either in base64, or as a publicly linked (you can set up "private" links as well) ContentVersion.
Base64
- Get PhotoUrl
- Get PageReference Content
- Save into field/return with other data
- Use on page with img tag
Downsides: Spotty support (never had a problem unless its an email), large amount of text, may need to return per request depending on how you set your page up (best used as a saved database entry instead of getting a value with each request)
ContentVersion
- Get the photo url
- Create new content version
- Upload Blob of User photo to the version
- Share using built in link feature
- Save link back to user (needs own context)
- Return Link Url with user record
- Display link on page
Downsides: Link is likely public to anyone with a copy of it, Setting this up to happen automatically is a pain, since you cant update a setup object & a non-setup object in the same context, Lots of extra objects involved to get the data to be shareable (ContentDocument, ContentVersion, User, etc)
Base64 Sample Code (Should run in dev console):
public static String GetBase64Content(String url) {
String Base64Content = 'data:image/png;base64, ';
if (url != null) {
PageReference imageSource = new PageReference(url);
Blob b = imageSource.getContent();
Base64Content += EncodingUtil.base64Encode(b);
}
return Base64Content;
}
User u = [
SELECT Id, FullPhotoUrl
FROM User
WHERE Id = :UserInfo.getUserId()
LIMIT 1
];
String content = GetBase64Content(u.FullPhotoUrl);
// use like:
// <img src="{!content}" />
You may need to store the base64 result either in a long/rich text field or in a json object, depending on how your server handles the data.
ContentVersion Demo
Creates a contentVersion using the blob from the users FullPhotoUrl, set to "publicly available", which creates a ContentDistribution, which you can query & get the ContentDownloadUrl from, which you can use in an img tag or store somewhere. Could be useful to create base64 or binary files on a target server.
private static ContentVersion CreateVersion(User u, Blob body) {
return new ContentVersion(
// https://salesforce.stackexchange.com/questions/117113/migrating-chatter-files-attached-to-feed-comments
OwnerId = UserInfo.getUserId(), // Has to be running user - won't be able to create otherwise..
VersionData = body,
Title = 'Profile Photo of ' + u.Name,
PathOnClient = 'profilePhoto.png',
Origin = 'H',
Profile_Image_Owner__c = u.Id,
Externally_Available__c = true,
FirstPublishLocationId = LibraryId // Used Custom setting w/ specific id, should replace with target dir
);
}
User u = [
SELECT Id, FullPhotoUrl
FROM User
WHERE Id = :UserInfo.getUserId()
LIMIT 1
];
ContentVersion version = CreateVersion(u, new PageReference(u.FullPhotoUrl).getContent());
insert version;
ContentDistribution dist = [
SELECT Id, DistributionPublicUrl, ContentDownloadUrl
FROM ContentDistribution
WHERE ContentVersionId = :version.Id
];
// Use like so:
// <img src="{!dist.ContentDownloadUrl}" />
// Need to update user to attach content url/id in a seperate context
// Could also save user id to document & query, or return id in response from server
Your tag seems correct, its your file path that seems to be off, mind sharing your zip file tree structure?
Did you zip the files directly, or you zipped a filder that contains these 3 files? if the later is your case, then your reference should be something similar to the below:
<ltng:require
styles="{!$Resource.fivestart+ 'toplevelfolder/rating.css'}"
scripts="{!$Resource.fivestart+ 'toplevelfolder/fivestar.js'}"
afterScriptsLoaded="{!c.scriptsLoaded}" />
Best Answer
Try going to settings > search for Remote Site Settings, click New Remote Site > Give it a Name, type in the FULL URL of the image including the 'http://' BE SURE to check the box: Disable Protocol Security since this is not an 'https', make it active and try your image then Good luck!