Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Salesforce - Is it possible to display image file from ContentVersion to custom visualforce page?

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>
like image 303
Meow Avatar asked Jan 20 '23 03:01

Meow


1 Answers

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.

like image 139
thegogz Avatar answered Jan 31 '23 09:01

thegogz