0

i want to read the csv data uploaded to backened.

for this i am sending the data via post from front end..

frontend code:

fileEvent(e) {
    this.filedata = e.target.files;
    if (this.filedata.length > 0) {
      const file: File = this.filedata[0];
      console.log(file);
      const formData: FormData = new FormData();
      formData.append('files', file, file.name);
      this.http.post('myUrl',  {file: formData}, this.options)
                     .subscribe((res) => {
                     });
    }
  } 

screenshot of my file: enter image description here

now on backened i have written route on api.js that directs me to the controller i have created.

my api.js code:

router.post('/product/csvdata', function (req, res) {
    productimport.importcsvProduct(req, res);
});

and finally on my controller i am consoling my data:

    var product = {
            importcsvProduct: function (req,res) {
                console.log(req.body.file);
             }
    };

module.exports = product;

but i am getting empty {} in console..??

can anyone check whats wrong with this..??

Deb
  • 265
  • 1
  • 3
  • 16
  • you may need [body-parser](https://www.npmjs.com/package/body-parser) as a middleware and configure it to accept form data – pandamakes Nov 16 '18 at 12:49
  • Is [this](https://stackoverflow.com/a/23843746/5534788) helps you to solve the problem? – lependu Nov 16 '18 at 12:49

1 Answers1

0

You need to use a file handling middleware in this case, such as multer.

const express = require('express')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
  // req.file is the `csvdata` file
  // req.body will hold the text fields, if there were any
})
Paul
  • 35,689
  • 11
  • 93
  • 122