Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 11, 2020 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/21b9ebec08ed93b3e7d0c469bb5963c4 to your computer and use it in GitHub Desktop.
Save parzibyte/21b9ebec08ed93b3e7d0c469bb5963c4 to your computer and use it in GitHub Desktop.
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