I created a function to download a zip file for multiple file for sharepoint.
function create_zip() {
var zip = new JSZip();
$.each(filePathArray, function (i, path) {
var filename = path; //"file" + i +".txt";
var filee = path.substring(path.lastIndexOf('/') + 1);
var fileURL = appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('" + filename + "')/$value?@target='" + hostweburl + "'";//$('#file').attr('href');
var request = $.ajax({
url: fileURL,
type: "GET",
contentType: "text/plain",
mimeType: 'text/plain; charset=x-user-defined' // <-[1]
});
request.done(function (data) {
//var filee = "MoveFiles" + count + ".txt";
zip.file(filee, data, { binary: true }); // <- [2]
//count++;
vfilecount++;
console.log(vfilecount);
console.log(vfilecount);
if (count == vfilecount) {
zip.generateAsync({ type: "base64" }).then(function (data) {
location.href = "data:application/zip;base64," + data;
});
}
});
});
}
Now This code works proper in Chrome and mozilla but not in IE . Please suggest any way.
As seen in https://github.com/Stuk/jszip/issues/376 (reposting it here to help others):
mimeType: 'text/plain; charset=x-user-defined'
doesn't work in IE 10. $.ajax
is meant to download text content, not binary content. Same with XmlHttpRequest without setting responseType. Browsers will try to decode the received content from UTF8 and corrupt it (because it's a binary content, not a text). Use a jQuery plugin (like jquery.binarytransport.js) or use a xhr directly (with xhr.responseType = "blob"
).location.href = "data:application/zip;base64," + ...
doesn't work in IE. Generate a blob instead and use saveAs
to trigger the download