0

I am sending excel in formdata() property from reactJs as follows

let formData = new FormData(), headers = {};

headers['Content-Type'] = 'multipart/form-data';
headers['Accept'] = 'application/json';

formData.append('dataFile', file);

let res = await axios({
    method: 'POST',
    headers,
    url: '/api/products/general-excel-import',
    data: formData
});

At my backend route is defined as

api.post(
  "/api/products/general-excel-import",
  [multer.any()],
  productController.generalExcelImport
);

And my controller function:

const excelToJson = require('convert-excel-to-json');

exports.generalExcelImport = async function(req, res) {
  const file = req.files[0];
  
  console.log({ file: file});

  const jsonData = excelToJson({sourceFile: file.buffer });
  return res.status(200).send({ data: jsonData, message: 'Imported successfully' });  
}

The console.log in controller outputs the following object in terminal

file: {
  fieldname: 'dataFile',
  originalname: 'findings.xlsx',
  encoding: '7bit',
  mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  buffer: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 62 ee 9d 68 5e 01 00 00 90 04 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... 9091 more bytes>,
  size: 9141
}

when the request is made at backend route it throws the following error

[0] TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of Object

can any one tell where i'm wrong in code

2
  • Try using source instead of sourceFile in the call to excelToJson. Commented Aug 16, 2022 at 5:27
  • @MosheKatz i tried that also, it the error changes to f.slice is not a function Commented Aug 16, 2022 at 5:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.