0

I have to create an upload function where admins can upload a report for a certain user.

I have access to this data

  • User ID (Number)
  • Report Type (Number)
  • Report (File)

When an an admin uploads a report, I want to create a directory if it doesn't exist yet like this

var location = __dirname + '/../../public/' + USERID + 'reports/' + filename;

I've been following this tutorial to upload a file and so this is the code I have

function uploadReport(req, res) {
    var fstream;
    var location = __dirname + '/../../public/reports/' + filename;
    if (req.busboy) {
        req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
            fstream = fs.createWriteStream(location);
            file.pipe(fstream);
            fstream.on('close', function () {
                console.log('file ' + filename + ' uploaded');
            });
        });
        req.busboy.on('finish', function () {
            res.json({
                success: true
            });
        });
        req.pipe(req.busboy);
    }
}

this is what I sent from my front end

------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="participant"

1
------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="type"

1
------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="file"; filename="test.pdf"
Content-Type: application/pdf


------WebKitFormBoundaryvS6HEnoxsgpmx646--

How can I extract participant and type from my form data and put them into useable variables?

Nicolas
  • 4,526
  • 17
  • 50
  • 87
  • Hmm, seems rather complex to upload a file and one value. This should be in the docs for busboy, so have a look there. But if I have to guess: have you checked the value of `fieldname` ? – Shilly Jul 05 '18 at 12:23
  • You can extract using `req.body.participant` – Subburaj Jul 05 '18 at 12:35
  • This code is a bit suspicious... you're allowing the users to write to wherever on disk, basically. – Brad May 03 '23 at 02:09

0 Answers0