I wrote one simple Visualforce page that let the user upload an image file then save the file to ContentVersion
object.
Now I want to display the saved image in my custom visualforce page. Is it even possible? Looks like <apex:image>
cannot be used. Also <img href="{!cv.contentVersion}"...>
had no luck.
The real problem is I did upload the image file successfully but what is the URL to it? I tested with random URL outside on google and I can display the image (like /../..some.jpg"). But I can't figure out what is the absolute URL for the image file that has been uploaded to contentversion.
NOTE: This is not static resource as my users may upload image to change their user image often.
Code
public with sharing class ImageUploadTestController {
public blob file { get; set; }
public String imageFilePath { get; set; }
public ContentVersion cv { get; set; }
public ImageUploadTestController() {
cv = [select id, versionData, title, pathOnClient FROM ContentVersion limit 1];
}
//fill out the inputFile field and press go. This will upload file to the server
public PageReference go() {
ContentVersion v = new ContentVersion();
v.versionData = file;
v.title = 'some title';
v.pathOnClient ='/foo.jpeg';
insert v;
return new PageReference('/' + v.id);
}
//v.id sample
//069A00000009Ux3
}//end class
Visualforce page
<apex:page controller="ImageUploadTestController">
<apex:form >
<apex:inputFile value="{!file}" />
<apex:commandbutton action="{!go}" value="go"/>
</apex:form>
<!-- none of below works!! :( -->
<a href="/{!cv.id}">{!cv.title} {!cv.pathOnClient}</a>
<a href="https://na7.salesforce.com/069A00000009FG4"></a>
<apex:image value="/069A00000009Ux3" width="220" height="55"/>
</apex:page>