1
votes

I tried to upload file as well as text data from the api,so i have write multipart api for getting file and text data from the res in nodejs for getting file i am using multer module but now i am getting undefined or null in req.files & req.file. I'm trying to upload a file using multer and below are the code snippets :

const express = require('express');
const app = express();
var multer = require('multer');
var upload = multer();

app.post('/api/upload', upload.single('photo'),function (req, res, next) {
    let data = req.body;
    let file = req.file;
    console.log(data)
    console.log(file)
    return res.status(200).send(data)
})

Response of this api always return null and undefined

functions: Beginning execution of "app"
>  [Object: null prototype] {}
>  undefined

and i am passing two parameters from the api

  1. photo which is a type of file
  2. name parameter which is type of string
    and i have pass this data from Form-data formate

hope anyone help my soon

1
Shouldn't it be req.body.file ? - Deekshith Hegde
I tried that but it will not resolve my probleam - Himanshu Sehgal
Where have you specified the destination of the file storage received from multer? - Deekshith Hegde

1 Answers

0
votes
var upload = multer({ dest: 'uploads/' })

Specify the destination file storage location for multer. The file can be then accessed in the same way as you are doing from req.file