function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naveen kumar 812Naveen kumar 812 

How do you use static resource in visual force page?

Deepali KulshresthaDeepali Kulshrestha
Hi Naveen,

Suppose there is a single file like any single image or standalone CSS file, that you need to refer in your VF page, then you can directly use the “$Resource.resourceName” to refer the static resource where ‘$Resource‘ is a global variable to use any static resource within visualforce page. 

Suppose you have an image file uploaded in static resource with name “Z_test”

<!-- An image can be referenced like this -->
<img src="<strong>{!$Resource.Z_Test}</strong>"/>

I suggest to visit this link ,it will help you 

http://www.sfdcpoint.com/salesforce/use-static-resource-in-visualforce/

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_resources_create.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Ajay K DubediAjay K Dubedi
Hi Naveen,

The way you reference a static resource in Visualforce markup depends on whether you want to reference a stand-alone file, or whether you want to reference a file that is contained in an archive (such as a .zip or .jar file):
To reference a stand-alone file, use $Resource.<resource_name> as a merge field, where <resource_name> is the name you specified when you uploaded the resource. For example:

<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>
or
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>

To reference a file in an archive, use the URLFOR function. Specify the static resource name that you provided when you uploaded the archive with the first parameter, and the path to the desired file within the archive with the second. For example:

<apex:image url="{!URLFOR($Resource.TestZip,'images/Bluehills.jpg')}" width="50" height="50"/>
or
<apex:includeScript value="{!URLFOR($Resource.LibraryJS, '/base/subdir/file.js')}"/>

You can use relative paths in files in static resource archives to refer to other content within the archive. For example, in your CSS file, named styles.css, you have the following style:
table { background-image: url('img/testimage.gif') }
When you use that CSS in a Visualforce page, you need to make sure the CSS file can find the image. To do that, create an archive (such as a zip file) that includes styles.css and img/testimage.gif. Make sure that the path structure is preserved in the archive. Then upload the archive file as a static resource named “style_resources”. Then, in your page, add the following component:

<apex:stylesheet value="{!URLFOR($Resource.style_resources, 'styles.css')}"/>
Since the static resource contains both the style sheet and the image, the relative path in the style sheet resolves and the image is displayed.
Through a custom controller, you can dynamically refer to the contents of a static resource using the <apex:variable> tag. First, create the custom controller:

global class MyController {

    public String getImageName() {

        return 'Picture.gif';//this is the name of the image

    }

}
Then, refer to the getImageName method in your <apex:variable> tag:
<apex:page renderAs="pdf" controller="MyController">
    <apex:variable var="imageVar" value="{!imageName}"/>
    <apex:image url="{!URLFOR($Resource.myZipFile, imageVar)}"/>
</apex:page>
If the name of the image changes in the zip file, you can just change the returned value in getImageName.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi