2

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

2 Answers2

0
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

Deekshith Hegde
  • 1,318
  • 2
  • 15
  • 27
0

Firstly you have to provide the storage location where you can specify storage of your files like this.

var upload = multer({ storage: location});

const location = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './your_folder_name')
}