Skip to content

Instantly share code, notes, and snippets.

@parzibyte

parzibyte/fotos.js

Created Jun 11, 2020
Embed
What would you like to do?
app.post('/fotos_producto', (req, res) => {
const form = formidable({
multiples: true,
uploadDir: DIRECTORIO_FOTOS,
});
form.parse(req, async (err, fields, files) => {
const idProducto = fields.idProducto;
for (let clave in files) {
const file = files[clave];
const nombreArchivo = file.name;
await productoModel.agregarFoto(idProducto, nombreArchivo)
}
});
form.on("fileBegin", (name, file) => {
const extension = path.extname(file.name);
const nuevoNombre = uuidv4().concat(extension);
file.path = path.join(DIRECTORIO_FOTOS, nuevoNombre);
file.name = nuevoNombre;
})
form.on("end", () => {
res.json({
respuesta: true,
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment