download multi templates files

i am a new developer in kintone. is it possible to click on the button and download multi files in kintone?there is not sample codes for me to understand.

thanks.

There was a sample code for doing this on the Japnaese kintone site.

This uses a javascript library called JSZip.

JSZip

The flow of downloading multiple files is like this:
Obtain Record Info > Create filekey list > Check file size > Create URL > Download Files > Compress Files > Save Files

Below is the sample code of it:

/* global JSZip */
/* global JSZipUtils */
/* global saveAs */
///
/// License
/// FileSaver.js MIT https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
/// jszip MIT or GPLv3 https://github.com/Stuk/jszip/blob/master/LICENSE.markdown
/// jszip-utils MIT or GPLv3 https://github.com/Stuk/jszip-utils/blob/master/LICENSE.markdown
///

(function() {
“use strict”;
var fieldCode = “Attachment File”; //Attachment file field code
var isGuestSpace = false;

//Function to obtain record (asynchronous) up to 100
function getAppRecords() {
var url = kintone.api.url("/k/v1/records", isGuestSpace);
var appId = kintone.app.getId();
var condition = kintone.app.getQueryCondition() || “”;
var query = condition + “order by $id asc”;
var body = {
“app”: appId,
“query”: query,
“field”: fieldCode
};
return kintone.api(url, “GET”, body);
}

//Function to obtain file key
function getFileKeys(json) {
var keys = [];
for (var i = 0; i < json.records.length; i++) {
var filetype = json.records[i][fieldCode];
// If multiple attachments exist
for (var j = 0; j < filetype.value.length; j++) {
keys.push(filetype.value[j]);
}
}
return keys;
}

//Function to check file size
function checkFileSize(filekeys) {
if (filekeys.length === 0) {
return kintone.Promise.reject(“Attachment do not exist”);
}
var totalsize = 0;
for (var i = 0; i < filekeys.length; i++) {
totalsize += parseInt(filekeys[i][“size”], 10);
}
if (totalsize < 999) { // Less than 1KB
totalsize = String(totalsize);
} else if (totalsize < 999999) { //Less than 1MB
totalsize = parseInt(totalsize / 1000, 10) + “K”;
} else if (totalsize < 999999999) { // Less than 1GB
totalsize = parseInt(totalsize / 1000000, 10) + “M”;
} else {
//Set the max file size to 1GB
return kintone.Promise.reject(“The file size is too big”);
}
var dflag = confirm(totalsize + “bytes will be downloaded. Are you sure?”);
if (!dflag) {
return kintone.Promise.reject(“Download has been cancelled”);
}
return filekeys;
}

// Function to obtain file url (asynchronous)
function addFileURL(key) {
return new kintone.Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
var params = {
“fileKey”: key[“fileKey”]
};
var url = kintone.api.urlForGet("/k/v1/file", params, isGuestSpace);
xhr.open(“GET”, url, true); //asynchronous
xhr.setRequestHeader(“X-Requested-With”, “XMLHttpRequest”);
xhr.responseType = “blob”;
xhr.onload = function() {
if (xhr.status === 200) {
var blob = new Blob([xhr.response]);
var wurl = window.URL || window.webkitURL;
var blobUrl = wurl.createObjectURL(blob);
key[“blobUrl”] = blobUrl; // Add URL to key record
resolve(key);
} else {
reject(JSON.parse(xhr.response));
}
};
xhr.send();
});
}

//Function to obtain multiple file URLs
function addfileURLs(filekeys, keynum) {
var opt_keynum = keynum || 0;
return addFileURL(filekeys[opt_keynum]).then(function(resp) {
opt_keynum++;
if (opt_keynum === filekeys.length) {
return filekeys;
}
return addfileURLs(filekeys, opt_keynum);
});
}

//Function to download a file asynchronously and put it into zip
function downloadFile(zip, url, filename) {
return new kintone.Promise(function(resolve, reject) {
// getbinarycontent is an API to obtain file from URL asynchronously
// Use JSZIP util API
JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) {
reject(err);
}
zip.file(filename, data, {binary: true});
resolve(data);
});
});
}

// Function to download multiple files
function downloadFiles(files, zip, filenum) {
var opt_zip = zip || new JSZip();
var opt_filenum = filenum || 0;
return downloadFile(opt_zip, files[opt_filenum][“blobUrl”], files[opt_filenum][“name”]).then(function(data) {
opt_filenum++;
if (opt_filenum === files.length) {
return opt_zip;
}
return downloadFiles(files, opt_zip, opt_filenum);
});
}

//Function to compress files in Zip
function doZipFile(zip) {
return zip.generateAsync({type: “blob”});
}

//Function to save to file
function saveZipFile(content) {
//Save using FileSaver.js
return saveAs(content, “example.zip”);
}

//Function called upon the button is clicked
function getZipFile() {
getAppRecords()
.then(getFileKeys)
.then(checkFileSize)
.then(addfileURLs)
.then(downloadFiles)
.then(doZipFile)
.then(saveZipFile)
.catch(function(error) {
alert(error);
});
}

//Place Button on the Record List
kintone.events.on(“app.record.index.show”, function(e) {
//Bug fix to prevent from multiplying
if (document.getElementById(“menuButton”) !== null) {
return;
}
var menuButton = document.createElement(“button”);
menuButton.id = “menuButton”;
menuButton.innerHTML = “Bulk Download”;
menuButton.onclick = function() {
getZipFile();
};
kintone.app.getHeaderMenuSpaceElement().appendChild(menuButton);
});
})();