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
ClaiborneClaiborne 

In Apex, Retrieve File Names from Static Resource Zip File

I have an application where the user creates a zip file of various images to be used on a Sites page, and then loads this as Static Resource.

 

Right now, to use the images, the user has to enter the exact, complete name (case matters) of the file name in the zip file to incorporate an image on the Sites Page. These specifications are maintained in a separate object.

 

What I would like to do is present the user with a picklist of the files in the static resource. This would eliminate a lot of errors.

 

But there does not seem to be a way to do this in Apex.

 

Any ideas? Anyone?

JimmyBeWhiteJimmyBeWhite

If it's possible for the user to make a list of the image names and load it with the pictures you can build a list with some logic like this. This takes a csv

 

public static String[] processCSVFile(String fileName){
           StaticResource sr = [Select  s.Name, s.Id, s.Body From StaticResource s where s.Name =:fileName];
           blob tempBlob = sr.Body;
           String tempString = tempBlob.toString();
           tempString = tempSTring.replace('"', '');
           String[] stringArray = tempString.split('\n');
           return stringArray;//returns an array of strings split by rows in a csv. 
//the rows can then be split by ',' to get the individual values
}

  You can build a selectoptions list based on the returned string array and then do your static resource query for the image based on the filename picked from the selectoptions. I'm not sure if its possible to get into the zip file and extract the filenames
Does that work? 

ClaiborneClaiborne

I had thought about something like this.

 

But I have no trust of users. I prefer, whenever possible, to limit the users to point and click and let the software handle everything else.

 

Actually, the more I investigated, I am probably going to have to stop using StaticResource for my particular application because end-user maintenance is tedious at best. The static resource itself has to be hard coded by name in the visualforce page. There is no way to pass the static resource as a variable.