0

So my main goal is that I can save images into user documents into mongoDB. So far I have been able to save the uploaded image to my servers temporary images folder and I can't for the life of me figure out how to push it to mongoDB.

Please note that this function will be used when a new user registers on my site and they will be able to upload a profile picture during the registration process.

So far I have done:

<form method="post" enctype="multipart/form-data" action="/file-upload">
<input type="file" name="thumbnail">
<input type="submit">

// we need the fs module for moving the uploaded files
 var fs = require('fs');

 app.post('/file-upload', function(req, res) {

    // get the temporary location of the file
    var tmp_path = req.files.thumbnail.path;

    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = './images/' + req.files.thumbnail.name;

    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;

        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files

        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
        });
      });
    });

The above code works and I can see the image being uploaded into my images folder on the server but how can I post this mongoDB?

Skywalker
  • 4,984
  • 16
  • 57
  • 122

0 Answers0