56 lines
1.6 KiB
JavaScript

function handleFileUpload() {
const file = document.getElementById('inputGroupFile01').files[0];
const reader = new FileReader();
reader.onload = function(e) {
const rawData = e.target.result;
var blocks = rawData.split('#####');
blocks.shift() // 1st element is empty
var data = {};
blocks.forEach(function (block) {
const split = block.indexOf('\n');
const table_name = block.substring(0, split);
const content = csvToObject(block.substring(split+1));
data[table_name] = content;
});
restore_request(data);
};
reader.readAsText(file);
}
async function restore_request(data) {
try {
// SELECT uuid, itemid, skuid, choice, attributes, image, show
const apiUrl = `${currentUrl}app/restore`;
const postData = {
data: data
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
} catch (error) {
console.error('Error fetching data item: ', error);
throw error;
}
}
// change file picker text on file upload
$(".custom-file-input").on("change", function() {
var fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
// Event listener restore from file
document.getElementById('submit_restore').addEventListener("click", handleFileUpload);