0

Im trying to figure out how to extract a file from a static resource. the static resource is zipped and contains a csv file. Im new with lwc, i have some experience with apex but i'm not a master.

I've been able to get the data from the csv by importing an unzipped version of the document with no trouble. the reason i'm using a zip file is so that i can include more csv files and pull the data as needed in my lightning component.

regarding the zip extraction. i've tried using apex which sort of works, i am able to select the body into a blob and then cast that data to a string after encoding it but what i get is an encrypted string. when i try to decode the string my data doesn't get passed to my lightning component.

regarding lwc i am able to import that static resource but all i can seem to get is a link to the files instead of the actual data in them.

Ive looked for a couple of days now and cant find anything that seems to work.

Is it possible to get usable data from a zipped csv file (static resource) via apex or some other method that's native to Salesforce?

Do you think i could ship the file to lwc and then use js to extract the files and store the string data in a local db or something like that?

Here is my code for the blob try. (i get the following log error as expected FATAL_ERROR System.StringException: BLOB is not a valid UTF-8 string)

//apex side
    @AuraEnabled(cacheable=true)
    public static String loadInterviewFile(String currentInterview){
        String queryCurrentInterview = 'select id,body, name from StaticResource Where Name = \''+currentInterview +'\'';
        StaticResource srObject = Database.query(queryCurrentInterview);
        blob tempBlob = srObject.Body;
        string s = EncodingUtil.base64Encode(tempBlob);   
        return EncodingUtil.base64Decode(s).toString();
//lwc side
import { LightningElement, api , track, wire} from 'lwc';
import getInterviewData from '@salesforce/apex/Core.loadInterviewFile';
export default class ListItem extends LightningElement {
    @api interviewFileName;
    @track interviewData; 

    @wire(getInterviewData , { currentInterview: '$interviewFileName' })
    InterviewDataRaw({ error, data }) {
        if (data) {
            this.data  = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.data  = undefined;
        }
        this.interviewData = this.data;
        console.log('Interview Resource data ' + this.interviewData);
    }
}

Any guidance you might have would be really appreciated.

Thanks

1 Answer 1

1

You can access specific file from the zip using the getContent method.

StaticResource sr = [SELECT Id,SystemModstamp FROM StaticResource WHERE Name = 'MyZipResource'];
String srPath = '/resource/' + sr.SystemModstamp.getTime() + '/MyZipResource/[FILEINZIP]'; 
PageReference pg = new PageReference(srPath);
String body = pg.getContent().toString();   //get the content as string
3
  • it worked. thank you so much Liron! this has been driving me crazy for 2 days. I really appreciate it. Commented Aug 20, 2020 at 15:23
  • I am getting a BLOB is not a valid UTF-8 string error using this, any suggestions? I have a JSON file Commented Aug 24, 2020 at 4:35
  • It should work when the file in zip is json. Suggest that you will open new post and add your code samples /zip/json files
    – Liron C
    Commented Aug 24, 2020 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .