-
-
Save parzibyte/56206940b2826bd3a8fd9471bc1a60b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://parzibyte.me/blog | |
*/ | |
// Elementos del DOM | |
const $inputArchivos = document.querySelector("#inputArchivos"), | |
$btnEnviar = document.querySelector("#btnEnviar"), | |
$estado = document.querySelector("#estado"); | |
$btnEnviar.addEventListener("click", async () => { | |
const archivosParaSubir = $inputArchivos.files; | |
if (archivosParaSubir.length <= 0) { | |
// Si no hay archivos, no continuamos | |
return; | |
} | |
// Preparamos el formdata | |
const formData = new FormData(); | |
// Agregamos cada archivo a "archivos[]". Los corchetes son importantes | |
for (const archivo of archivosParaSubir) { | |
formData.append("archivos[]", archivo); | |
} | |
// Los enviamos | |
$estado.textContent = "Enviando archivos..."; | |
const respuestaRaw = await fetch("./guardar.php", { | |
method: "POST", | |
body: formData, | |
}); | |
const respuesta = respuestaRaw.json(); | |
// Puedes manejar la respuesta como tú quieras | |
console.log({ respuesta }); | |
// Finalmente limpiamos el campo | |
$inputArchivos.value = null; | |
$estado.textContent = "Archivos enviados"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment