Hi, I'm trying to upload a file, in my case it will mostly be an image or images in a director of my backend made in nodejs, then I leave the code of the function that subverts the image with formidable :
router.post('/uploadImage', (request, response) => {
const form = new formidable.IncomingForm()
const address = path.dirname(__filename).split('/')
address.pop()
form.uploadDir = '../imagenes/'; //path donde guardaras la imagines
form.keepExtensions = true; //mantener las extensiones
form.maxFieldsSize = 10 * 1024 * 1024; //10MB
form.multiples = true; //multiples archivos
form.parse(request, (err, fields, files) => {
if (err) {
response.json({
message: 'error al subir la imagen',
data: []
})
}
const { upload } = files //Hacemos Destructuring, por lo que equivale a 'const upload = files.upload'
let data
if (upload) { //Si existe archivo subido o no
let filesPath
if (Array.isArray(upload)) { //Si se han subido mas de un archivo, es decir, si es un Array de archivos
filesPath = upload.map(file => file.path)
} else {
filesPath = upload.path
}
data = {
message: 'imagen subida correctamente !!',
data: filesPath, //Devolvemos una o la lista de rutas de los archivos
numImagenes: upload.length //Devolvemos la cantidad de archivos
}
} else {
data = {
message: 'No hay ninguna imagen seleccionada para subir .. ',
data: [],
numImagenes: 0
}
}
response.json(data)
})
})
Below I show what the server returns to me when using postman to test whether it works or not, and this is what it says to me: