Search code examples
node.jsexpressmultipartform-databusboy

express-busboy. Sending files


I want to send a file with express-busboy. On the readme it says that you can use req.body. So i did. The regular inputs worked, but the file inputs just returned the name of the file i uploaded. I uploaded the file called myfile.png and in the textinput, i put the value of 'Hello World'. Here is a sample of code:

//js:

const express = require('express');
const mysql = require('mysql');
const bp = require('body-parser');
const session = require('express-session');
const uc = require('upper-case');
const busboy = require('express-busboy');
const fs = require('fs');
const util = require('util');
const app = express();

app.use(bp.urlencoded({ extended: true }));
busboy.extend(app, {
    upload: true,
    path: './mydir(folder)'
});

app.post('/somewhere', (req, res) => {
  console.log(req.body.textinput);
  //returns the 'Hello World'
  console.log(req.body.imginput);
  //returns 'myfile.png'
});
<html>

<body>
  <form action="/somewhere" method="post" enctype="multipart/form-data">
    <input type="text" name="textinput">
    <input type="file" name="imginput" accept='image/*'>
  </form>
</body>

</html>


Solution

  • You have to use req.files instead of req.body.