# download multi templates files

**URL:** <https://forum.kintone.dev/t/download-multi-templates-files/42>\
**Category:** General\
**Created:** [December 25, 2017, 5:16am UTC](https://forum.kintone.dev/t/download-multi-templates-files/42 "2017-12-25T05:16:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [December 25, 2017, 5:16am UTC](https://forum.kintone.dev/t/download-multi-templates-files/42/1 "2017-12-25T05:16:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [December 26, 2017, 8:28pm UTC](https://forum.kintone.dev/t/download-multi-templates-files/42/2 "2017-12-26T20:28:40Z")

</div>

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

This uses a javascript library called JSZip.

### [JSZip](http://stuk.github.io/jszip/)

- **URL:** [https://js.cybozu.com/jszip/v3.1.5/jszip.min.js](https://js.cybozu.com/jszip/v3.1.5/jszip.min.js)

- [JSZip(v3.0.0)](http://stuk.github.io/jszip/)&nbsp;… Used to zip file

- [JSZipUtils(v0.0.2)](https://github.com/Stuk/jszip-utils)&nbsp;… Used when downloading file

- [FileSaver.js(v1.0.0)](https://github.com/eligrey/FileSaver.js)&nbsp;… Used when saving file

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](https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md)  
/// jszip MIT or GPLv3 [https://github.com/Stuk/jszip/blob/master/LICENSE.markdown](https://github.com/Stuk/jszip/blob/master/LICENSE.markdown)  
/// jszip-utils MIT or GPLv3 [https://github.com/Stuk/jszip-utils/blob/master/LICENSE.markdown](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];  
//&nbsp;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);  
});  
});  
}

//&nbsp;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);  
});  
})();
