将图像从服务器上的“/ images /”文件夹推送到mongoDB

时间:2016-01-19 15:02:06

标签: javascript image mongodb mlab

所以我的主要目标是将图像保存到mongoDB中。 到目前为止,我已经能够将上传的图像保存到我的服务器临时图像文件夹中,我不能为我的生活找出如何将其推送到mongoDB。

请注意,当新用户在我的网站上注册并且他们将能够在注册过程中上传个人资料图片时,将使用此功能。

到目前为止,我已经完成了:

<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');
        });
      });
    });

以上代码有效,我可以看到图片被上传到服务器上的images文件夹中,但是如何发布这个mongoDB?

0 个答案:

没有答案