0

Here is my code.I am able to download the zip file, but now i want to set a password to it using javascript/jquery.

    function saveAsZip(fileContents, fileName) {

    var zip = new JSZip();
    zip.file(fileName, fileContents);
    var content = zip.generate();
    var link = document.createElement('a');
    var linkName = fileName.replace('.XML', '')
    link.download = linkName + '.zip';
    link.href = "data:application/zip;base64," + content;
    link.click();   

}
Suhail Mushtaq
  • 81
  • 1
  • 2
  • 11
  • http://www.pageresource.com/jscript/jpass.htm – brk Jan 04 '17 at 06:04
  • Possible duplicate of [How to lock/password-protect an XML file?](http://stackoverflow.com/questions/12216247/how-to-lock-password-protect-an-xml-file) – Yaje Jan 04 '17 at 06:04
  • @user2181397 i want to set a password for my file to download. I am not interested in Adding a password to a page – Suhail Mushtaq Jan 04 '17 at 06:22
  • So ..you don't want to password protect the file ..but the PAGE ..or gateway link – Robert Hoffmann Jan 04 '17 at 10:32
  • @Robert You are getting it wrong sir. I want to password protect the zip file. – Suhail Mushtaq Jan 04 '17 at 10:52
  • JSZip (client-side), does not support password protection. You'll need to create your zip server-side with some other method (php, .net, whatever). A workaround would be: user clicks link ..you ask for password to proceed to download ..if ok ..serve up your zip. – Robert Hoffmann Jan 04 '17 at 13:02

2 Answers2

3

Actually, this is just not possible so far, as stated here:

https://github.com/Stuk/jszip/issues/291

If you use node.js on linux, this question might be a way to achieve it: How do I password protect a zip file in Nodejs?

Community
  • 1
  • 1
Dominik
  • 2,801
  • 2
  • 33
  • 45
1

I ended up using Ionic.Zip in Dot Net. Here is my html and javascript code

<div>
    <input type="password" id="pass" placeholder="Set Password" />
    <button type="submit" class="cutomDownloadCCDA">Zip</button>
</div>
<script>
$('.cutomDownloadCCDA').click(function(e) {
    //window.location.href = "Home/Zip";// Simple Way

    var password = $('#pass').val();
    $('#downloadFrame').remove(); // This shouldn't fail if frame doesn't exist
    $('body').append('<iframe id="downloadFrame" style="display:none"></iframe>');
    $('#downloadFrame').attr('src', '/Home/Zip?password=' + password);
});
</script>

Here is the action Method of MVC

public void Zip(string password)
    {
        using (ZipFile zip = new ZipFile())
        {
            string xml = "Your XML Data";

            var newStream = new MemoryStream();
            var newWriter = XmlWriter.Create(newStream);
            newWriter.WriteRaw(xml);
            newStream.Position = 0;
            newWriter.Flush();
            newStream.Seek(0, SeekOrigin.Begin);

            // Ist File
            ZipEntry e = zip.AddEntry("test.xml", newStream);
            e.Password = password;
            e.Encryption = EncryptionAlgorithm.WinZipAes256;

            // 2nd File    
            //ZipEntry f2 = zip.AddEntry("test1.xml", newStream);
            //f2.Password = "456";
            //f2.Encryption = EncryptionAlgorithm.WinZipAes256;

            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "attachment;filename=somefile.zip");
            zip.Save(Response.OutputStream);
        }
    }
n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35
Suhail Mushtaq
  • 81
  • 1
  • 2
  • 11