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
pcalpcal 

Accessing content of static resources from within Apex code

Hi all.  Quick question: is there any way to access the content of a static resource from Apex code?

I know how to access the resource inside VF markup (URLFOR, $Resource); what I'm trying to do here is get at the contents of an XML or text file resource from within an Apex method.

I couldn't find an API for doing this.  The only thing I could think of was to do an HTTP callout, but that seems to give me a 404 no matter what I do; I gather there is a proxy back there that has a different view of the world than my browser does.

Is there a better way to go about this?

Thanks very much,
-p

Best Answer chosen by Admin (Salesforce Developers) 
pcalpcal
Ah, never mind, answered my own question.  Was so simple I just missed it:

Code:
  PageReference pr = new PageReference('/resource/MyResourceZip/foo.xml');
  Blob content = pr.getContent();

 

All Answers

pcalpcal
Ah, never mind, answered my own question.  Was so simple I just missed it:

Code:
  PageReference pr = new PageReference('/resource/MyResourceZip/foo.xml');
  Blob content = pr.getContent();

 

This was selected as the best answer
JasonH_AllerganJasonH_Allergan

I have a question,

 

I have a flash SWF file that is referencing an XML - both are static resources. DO you know how to get the flashfile to look at the XML resource? i have done a direct Link in the actionscript but it just does not want to load it.

 

Any ideas?

 

Jason

Yasa Kusuma 3Yasa Kusuma 3
Does the answer above still work? It does work from dev console, but when fired from a trigger I get a 302 redirect :( 
Syed Subhan 9Syed Subhan 9
Hi pcal. Can you please send me full code as I am working on the same module, I need to retrieve the fields from static resource file and store back the same field data onto same static resource document. Thanks.