Library JSZip does not work outside of its official website

1

I need to use the JSZip library or its JSZip-utils variant to compress files (in my case external via URL), but the strange thing is that no matter how much the library, use the example code in my project as well as in pages jsfiddle type, it does not work since it never throws the message for download. It must be said that the original code of the page has 2 lines where the compression of image or files is set which in my example remove them and only leave the line that creates a TXT that is the simplest thing out there, just to see if it worked with the simplest but still does not work.

link

 var zip = new JSZip();
 zip.file("Hello.txt", "Hello World\n"); //Crea archivo Hello con frase Hello world.
 var img = zip.folder("images"); //seteo de carpeta de imagenes
 img.file("smile.gif", imgData, {base64: true}); //compresion de archivos de imagen.
 zip.generateAsync({type:"blob"})
 .then(function(content) {
 // see FileSaver.js
 saveAs(content, "example.zip");
 });

In the project I put some alerts to see where the function went and although it goes through the final compression, it does not open the download popup

link

Could someone help me because the message is not generated for the download?

Thank you very much !!

    
asked by user3674768 20.08.2018 в 14:30
source

1 answer

0

The problem is that the saveAs function is not defined.

Solution:

You can find it here https://stuk.github.io/jszip/vendor/FileSaver.js

Original repository : FileSaver.js

Demo

<!DOCTYPE html>


<html>

<script src="https://kripken.github.io/sql.js/js/sql.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.js"></script>
<script src="https://stuk.github.io/jszip/vendor/FileSaver.js"></script>

<body>

<button onclick="zip()">Descargar</button>

<script>

function zip(){
var zip = new JSZip();

// Agregamos contenido en un archivo de texto de alto nivel.
zip.file("Hola.txt", "Hola Mundo\n");
alert("Creo Archivo de texto");

// Generar el archivo .zip de forma asíncrona.
zip.generateAsync({type:"blob"}).then(function(contenido) {
alert("Entra a guardado de archivo");
    saveAs(contenido, "archivo.zip");
});}
</script>

</body>
</html>
    
answered by 20.08.2018 / 15:17
source