<style>
.mbody {
display: flex;
align-items: center;
justify-content: center;
background: #B13403;
}
.drag-area-page {
border: 2px dashed #fff;
height: 400px;
width: 700px;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.drag-area-page.active {
border: 2px solid #fff;
}
.drag-area-page .icon {
font-size: 100px;
color: #fff;
}
.drag-area-page header {
font-size: 24px;
font-weight: 500;
color: #fff;
}
.drag-area-page span {
font-size: 22px;
font-weight: 500;
color: #fff;
margin: 10px 0 15px;
}
.drag-area-page button {
padding: 10px 25px;
font-size: 20px;
font-weight: 500;
border: none;
outline: none;
background: #fff;
color: #5256ad;
border-radius: 5px;
cursor: pointer;
}
.drag-area-page img {
height: 100%;
width: 100%;
object-fit: cover;
border-radius: 5px;
}
</style>
<div class="modal fade" id="{{ modal }}" tabindex="-1" aria-labelledby="pageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="pageModalLabel">Importer fichier CSV</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="{{ path }}" method="POST" id="mform_id" onsubmit="return mySubmitFunction{{modal}}(event)">
<div class="modal-body mbody" style="max-height: 48rem;">
<div class="drag-area-page">
<div class="icon">
<i class="fas fa-cloud-upload-alt"></i>
</div>
<header>Drag & Drop pour télécharger le fichier</header>
<span>OU</span>
<button type="button">Parcourir le Fichier</button>
<input type="file" name="fileToUpload" id="fileToUpload" accept="text/csv" multiple hidden>
</div>
</div>
<div class="modal-footer modal-footer-page">
<input type="text" name="mfileToUpload" id="mfileToUpload" hidden>
<input type="text" name="mfileNameToUpload" id="mfileNameToUpload" hidden>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary" onclick="return mySubmitFunctionbtn{{modal}}(event)">Importer</button>
</div>
</form>
</div>
</div>
</div>
<script>
let submit{{modal}} = false;
let mdocument{{modal}} = document.querySelector("#{{modal}}");
// selecting all required elements
let modalf{{modal}} = mdocument{{modal}}.querySelector(".modal-footer-page"),
minput{{modal}} = modalf{{modal}}.querySelector("input"),
mnameinput{{modal}} = modalf{{modal}}.querySelector("#mfileNameToUpload");
let dropArea{{modal}} = mdocument{{modal}}.querySelector(".drag-area-page"),
dragText{{modal}} = dropArea{{modal}}.querySelector("header"),
button{{modal}} = dropArea{{modal}}.querySelector("button"),
input{{modal}} = dropArea{{modal}}.querySelector("input");
let file{{modal}}; // this is a global variable and we'll use it inside multiple functions
button{{modal}}.onclick = () => {
input{{modal}}.click(); // if user click on the button then the input also clicked
};
input{{modal}}.addEventListener("change", function () { // getting user select file and [0] this means if user select multiple files then we'll select only the first one
file{{modal}} = this.files;
dropArea{{modal}}.classList.add("active");
// file.forEach(showFile);
showFile{{modal}}(); // calling function
});
// If user Drag File Over DropArea
dropArea{{modal}}.addEventListener("dragover", (event) => {
event.preventDefault(); // preventing from default behaviour
dropArea{{modal}}.classList.add("active");
dragText{{modal}}.textContent = "Libérer pour télécharger le fichier";
});
// If user leave dragged File from DropArea
dropArea{{modal}}.addEventListener("dragleave", () => {
dropArea{{modal}}.classList.remove("active");
dragText{{modal}}.textContent = "Drag & Drop pour télécharger le fichier";
});
// If user drop File on DropArea
dropArea{{modal}}.addEventListener("drop", (event) => {
event.preventDefault();
// preventing from default behaviour
// getting user select file and [0] this means if user select multiple files then we'll select only the first one
file{{modal}} = event.dataTransfer.files;
input{{modal}}.files = event.dataTransfer.files;
// file.forEach(showFile);
showFile{{modal}}(); // calling function
});
function showFile{{modal}}() {
minput{{modal}}.value = [];
mnameinput{{modal}}.value = [];
dragText{{modal}}.innerHTML = '';
for (let index = 0; index < file{{modal}}.length; index++) {
// const element = file[index];
// console.log(file[index]);
let fileType = file{{modal}}[index].type;
// getting selected file type
// let fileURL = fileReader.result;
let validExtensions = ["text/csv", "application/vnd.ms-excel"]; // adding some valid image extensions in array
if (validExtensions.includes(fileType)) { // if user selected file is an image file
let fileReader = new FileReader(); // creating new FileReader object
fileReader.onload = () => {
let fileURL = fileReader.result;
let imgTag = `Fichier déposé avec succès <br>`;
filename = file{{modal}}[index].name;
dragText{{modal}}.innerHTML += filename + ' , ';
minput{{modal}}.value += "|[|" + fileURL;
mnameinput{{modal}}.value += "|[|" + filename;
};
fileReader.readAsDataURL(file{{modal}}[index]);
// submit = true;
} else {
alert("Ceci n'est pas un fichier CSV!");
alert(fileType);
dropArea{{modal}}.classList.remove("active");
dragText{{modal}}.textContent = "Drag & Drop pour télécharger le fichier";
submit{{modal}} = false;
minput{{modal}}.value = [];
mnameinput{{modal}}.value = [];
break;
}
}
}
function mySubmitFunctionbtn{{modal}}(e) {
submit{{modal}} = true;
mdocument.getElementById("mform_id").submit();
}
function mySubmitFunction{{modal}}(e) {
if (!submit{{modal}}) {
e.preventDefault();
}
}
</script>