Se agregó funciones de carga y descarga

This commit is contained in:
evenegas
2025-06-17 09:20:52 -06:00
parent 30944dee62
commit 3817e7bd4b
3 changed files with 98 additions and 44 deletions
+28 -12
View File
@@ -12,19 +12,37 @@ const CargaArchivo: React.FC = () => {
const handleUpload = async () => {
if (!archivo) return;
const token = localStorage.getItem("token"); // 👈 Token JWT guardado en login
if (!token) {
alert("No hay token disponible. Inicia sesión.");
return;
}
const formData = new FormData();
formData.append("archivo", archivo);
formData.append("file", archivo); // 👈 ¡Debe coincidir con FileInterceptor('file')
const res = await fetch("/api/cargar-archivo", {
method: "POST",
body: formData,
});
try {
const res = await fetch("http://localhost:4000/excel/load", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`, // 👈 Incluye el token en el header
},
body: formData,
});
if (res.ok) {
alert("Archivo subido correctamente");
setArchivo(null);
} else {
alert("Error al subir archivo");
const data = await res.json();
if (res.ok) {
alert("Archivo subido correctamente");
setArchivo(null);
} else {
console.error(data);
alert(`Error al subir archivo: ${JSON.stringify(data)}`);
}
} catch (err) {
alert(`Error en la solicitud: ${err}`);
}
};
@@ -41,10 +59,8 @@ const CargaArchivo: React.FC = () => {
disabled={!archivo}
className="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700"
style={{
backgroundColor: "#2563eb",
color: "#fff",
}}
>
Subir archivo