72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
const wordButton = document.getElementById("word-button");
|
|
const excelButton = document.getElementById("excel-button");
|
|
const lunchIdSelect = document.getElementById("LunchId");
|
|
const wordMIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
const excelMIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
|
|
if (wordButton) {
|
|
wordButton.addEventListener("click", () => {
|
|
saveList("docx");
|
|
console.log("сохранить в docx")
|
|
});
|
|
}
|
|
|
|
if (excelButton) {
|
|
excelButton.addEventListener("click", () => {
|
|
saveList("xlsx");
|
|
});
|
|
}
|
|
|
|
const saveList = async function (fileType) {
|
|
let lunchIdSelect = document.getElementById("LunchId");
|
|
if (!lunchIdSelect) {
|
|
console.log("Element with id 'LunchId' not found.");
|
|
return;
|
|
}
|
|
|
|
let listModel = {
|
|
"LunchId": Array.from(lunchIdSelect.selectedOptions).map(option => option.value),
|
|
"FileType": fileType
|
|
};
|
|
|
|
$.ajax({
|
|
url: `/home/SaveReport`,
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
headers: { "Content-Type": "application/json" },
|
|
data: JSON.stringify(listModel)
|
|
}).done((file) => {
|
|
let byteArray = new Uint8Array(file);
|
|
saveFile(byteArray, fileType);
|
|
});
|
|
};
|
|
|
|
const saveFile = async function (bytes, fileType) {
|
|
if (window.showSaveFilePicker) {
|
|
let type;
|
|
if (fileType == "docx") {
|
|
type = {
|
|
description: "Microsoft Word (OpenXML)",
|
|
accept: { [wordMIME]: [".docx"] }
|
|
};
|
|
} else if (fileType == "xlsx") {
|
|
type = {
|
|
description: "Microsoft Excel (OpenXML)",
|
|
accept: { [excelMIME]: [".xlsx"] }
|
|
};
|
|
}
|
|
const opts = {
|
|
suggestedName: `equipment-purchase-list.${fileType}`,
|
|
types: [type],
|
|
};
|
|
try {
|
|
const handle = await showSaveFilePicker(opts);
|
|
const writable = await handle.createWritable();
|
|
await writable.write(bytes);
|
|
await writable.close();
|
|
} catch (error) {
|
|
console.error("Error saving file:", error);
|
|
}
|
|
}
|
|
};
|