Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My requirement is to zip the content in client side controller using javascript library JSZIP, i have included it as a static resource. Below is the code.

Component:

<aura:component >

    <ltng:require scripts="/resource/jszip" afterScriptsLoaded="{!c.onPress}"/>

</aura:component>

Controller:

({
    onPress : function(component) {
        var packageXml = '<?xml version="1.0" encoding="UTF-8"?>' + 
            '<Package xmlns="http://soap.sforce.com/2006/04/metadata">' +
            '<version>35.0</version>' +
            '</Package>' ;

        var zipFile = new JSZip();
        zipFile.file('package.xml',packageXml,{base64: 'true'});
        var data = zipFile.generate();
        alert(data);
    }
})

I click on Press button, while locker service is enabled I am facing issue and when Locker service disabled the alert is showed as expected (attached the outputs).

Locker service enabled: enter image description here

Locker service disabled: enter image description here How to make this work when locker service is enabled. Thank you for help.

The error is very cryptic and not helpful

enter image description here

share|improve this question
up vote 1 down vote accepted

You just forgotten to declare the variable zipFile, change the line

zipFile = new JSZip();

to this line:

var zipFile = new JSZip();

also I will recommend you to use the Salesforce Lightning CLI to check this kind of issues, its a heroku command line tool that check for this errors.

Follow this steps to install Salesforce Lightning CLI: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/cli_intro.htm


UPDATE: In my personal org I tested the code you provided with a few modifications in an aura application and it worked fine. Here is the code:

App:

<aura:application >
    <ltng:require scripts="/resource/jszip/jszip.min.js" afterScriptsLoaded="{!c.onPress}"/>
</aura:application>

Js controller:

  ({
        onPress : function(component) {
            var packageXml = '<?xml version="1.0" encoding="UTF-8"?>' + 
                '<Package xmlns="http://soap.sforce.com/2006/04/metadata">' +
                '<version>35.0</version>' +
                '</Package>' ;

            var zipFile = new JSZip();
            zipFile.file('package.xml',packageXml,{base64: 'true'});
            var data = zipFile.generateAsync();
            alert(data);
        }
    })

I downloaded the last version (3.0.0) of jszip from their site and had the issue that the method generate() is deprecated, so I changed it to generateAsync() as is suggested in the upgrade guide.

share|improve this answer
    
Hi Cruz, the issue still exists even after adding the line you mentioned. – Pradeep Chary Aug 1 at 4:02
    
First I recommend you to use the Salesforce Lightning CLI to check and fix all similar issues like this, maybe there is other code that has the same issue. I tried to test your code in my person al org and I make it work, I will update my response. – D. Cruz Aug 1 at 12:39
    
Thank you for your support. It worked with (3.0.0) version of jszip. { onPress : function(component,event,helper) { var packageXml = '<?xml version="1.0" encoding="UTF-8"?>' + '<Package xmlns="soap.sforce.com/2006/04/metadata">'; + '<version>35.0</version>' + '</Package>' ; var data; var zipFile = new JSZip(); zipFile.file('package.xml',packageXml); zipFile.generateAsync({type:"base64"}).then(function(content‌​) { helper.contentMethod(component,content); }); }, } – Pradeep Chary Aug 4 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.