ken_nogi/pleasanter/develop/★MSS/★マスターシート/sites/scripts/9.添付ファイルDL.js
Kenichiro NOGI 7176dc33ec pleasanter
2025-07-10 10:15:35 +09:00

157 lines
4.9 KiB
JavaScript

/*
@config : next999.json
@filename : ${psFileBasename}
@title : 9.添付ファイルDL
@name :
@siteIds : 189112
@siteTitles : ★マスターシート
@disabled: false
*/
/*$.getScript("https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js");
$.getScript("https://cdnjs.cloudflare.com/ajax/libs/axios/1.3.4/axios.min.js");
$.getScript("https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js");
$p.events.on_editor_load = function () {
attachFileCheckbox('AttachmentsA');
}
*/
function attachFileCheckbox(attach) {
let title = '';
if ($('#Results_Class200').attr('data-readonly') == '1') {
title = $('#Results_Class200').text();
} else {
title = $('#Results_Class200').val();
}
let colName = $('#Results_' + attach + 'Field label')[0].innerHTML;
let attachData = JSON.parse($p.getControl(attach).val());
let zipFilename = title + '■' + colName + '■';
//console.log(attachData.length);
if (attachData.length > 1) {
$.each(attachData, function (index, value) {
let checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.style.margin = '2px';
checkbox.style.float = 'left';
checkbox.name = attach + '_checkbox';
checkbox.id = value.Guid + '_checkbox';
checkbox.value = value.Guid;
$('#' + value.Guid).prepend(checkbox);
});
//ダウンロードボタン
let div = document.createElement('div');
let dlButton1 = document.createElement('button');
dlButton1.type = 'button';
dlButton1.id = attach + '_dlButton1';
dlButton1.style.marginRight = '5px';
dlButton1.innerHTML = '全ファイルDL';
dlButton1.setAttribute('attach', attach);
dlButton1.setAttribute('onclick', 'dlAllFiles("' + attach + '","' + zipFilename + '全ファイルDL' + '")');
let dlButton2 = document.createElement('button');
dlButton2.type = 'button';
dlButton2.id = attach + '_dlButton2';
dlButton2.style.marginRight = '5px';
dlButton2.innerHTML = '選択DL';
dlButton2.setAttribute('attach', attach);
dlButton2.setAttribute('onclick', 'dlSelectedFiles("' + attach + '","' + zipFilename + '選択DL' + '")');
div.style.margin = '5px';
div.style.marginTop = '0px';
div.style.marginLeft = '120px';
div.style.marginRight = '5px';
div.style.marginBottom = '10px';
div.style.clear = 'both';
div.append(dlButton1);
div.append(dlButton2);
$('#Results_' + attach + 'Field').after(div);
$('#' + attach + '_dlButton1').button({
icon: 'ui-icon-document'
});
$('#' + attach + '_dlButton2').button({
icon: 'ui-icon-document'
});
}
}
function dlSelectedFiles(attach, zipFilename) {
//console.log(zipFilename);
//チェックがついているもののみ
const fileList = $('input[name="' + attach + '_checkbox"]:checked');
let guidList = new Array();
for (item of fileList) {
//console.log(item);
guidList.push(item.value);
}
if (guidList.length > 0) {
dlZipAttachements(guidList, zipFilename);
}
}
function dlAllFiles(attach, zipFilename) {
let guidList = new Array();
//全部
const fileList = JSON.parse($p.getControl(attach).val());
for (item of fileList) {
guidList.push(item.Guid);
}
if (guidList.length > 0) {
dlZipAttachements(guidList, zipFilename);
}
}
/**
* 複数ファイルを1ファイルにzip圧縮してDLする
* @param guidList DLする添付ファイルのGUIDのリスト
* @param zipFilename zipファイル名
* @return なし
*/
async function dlZipAttachements(guidList, zipFilename) {
var binary;
var filename;
const zip = new JSZip();
const jsonReq = {
ApiVersion: 1.1
};
for (guid of guidList) {
await axios.post("/pleasanter/api/binaries/" + guid + "/get", jsonReq)
.then(function (response) {
binary = decodeBase64(response.data.Response.Base64);
filename = response.data.Response.FileName;
zip.folder(zipFilename).file(filename, binary, {
binary: true
});
})
.catch(function (error) {
console.log(error);
})
.finally(function () { });
}
saveAs(await zip.generateAsync({
type: "blob"
}), zipFilename + ".zip");
}
/**
* base64デコード処理
* @param base64 base64文字列
* @return デコードしたバイナリデータ
*/
function decodeBase64(base64) {
const bin = atob(base64.replace(/^.*,/, ''));
var buffer = new Uint8Array(bin.length);
for (var i = 0; i < bin.length; i++) {
buffer[i] = bin.charCodeAt(i);
}
return buffer.buffer;
}