I wrote one simple Visualforce page that let 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>
                I don't believe its possible to serve it from content currently The format provided by ScottW works for Documents.
The other option I've done is upload a zip file containing my images to the Static Resources which can then be referenced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With