Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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>

1 Answer

0 votes
by (32.1k points)

I doubt for this to be possible to serve from the content presented. The main reason is that the format provided by you works only for documents not for images.

Another way you can make this work is by uploading a zipped file which contains images to the Static Resources which can actually be referenced.

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce administrator certification.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...