0

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'. I want the send the file that i upload to a folder called mydir (which is in ./). 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'
});

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>
  • `req.body` will contain the extra data (text fields) for your input, what you are looking for is in `req.files` – SylvainF Mar 05 '19 at 12:06
  • I dont understand can you give an example? I switched req.body.imginput to req.files.imginput, but it isnt sending the file over to my folder. –  Mar 05 '19 at 12:11
  • I was refering to your console.log -> if you want to print your file(s) you should `console.log(req.files)`. It will be an array of the file(s) you upload. In your case you should also check the path you upload path you set. Try `/mydir` instead of `./mydir` – SylvainF Mar 05 '19 at 13:26

0 Answers0