简体   繁体   English

从客户端网页上传图片

[英]Upload an image from a client-side webpage

One of the modules in our project is to upload an image from a webpage to mongo database using nodejs. 我们项目中的模块之一是使用nodejs将图像从网页上传到mongo数据库。 We have completed connecting to a mongo database and upload an image using physical location of the image on the system, but we are not able to make the upload dynamic from a webpage. 我们已经完成了连接到mongo数据库的操作,并使用系统上图像的实际位置上传了图像,但是我们无法使动态上传来自网页。

We convert the image to a base64 code and then save it to the database. 我们将图像转换为base64代码,然后将其保存到数据库。 MongoDB returns a unique id. MongoDB返回唯一的ID。 We want to integrate this process and make it dynamic. 我们想整合这个过程并使之动态。 The code we used to connect to mongoDB and upload an image from physical location is available here. 我们用于连接到mongoDB并从物理位置上传图像的代码在此处提供。

var MongoClient = require('mongodb').MongoClient, 
    format = require('util').format,
    fs = require('fs'),
    http = require('http');

http.createServer(function (req, res) {

    //should be triggered by the user upload button
    put();

    //triggered after the upload/button click
    res.writeHead(200, {'Content-Type': 'text/html'});

    MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
        if(err) throw err;


        var collection = db.collection('test_insert');

        collection.find().toArray(function(err, results) {
            //console.dir(results);
            // Let's close the db
            //ret = results[0];

            console.log(results[0]);
            res.end('<img alt="sample" src="data:image/png;base64,' +  results[0].image + '">');
            db.close();
        });
    });


    //res.end("Hello World\n");
}).listen(3030);

function read() {
    var image_base64 = fs.readFileSync('./uploads/2088-1nqsb3l.jpg').toString('base64');

    return image_base64;
    //console.log(base64_data);
}


function put() {
    MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
        if(err) throw err;

        var collection = db.collection('test_insert');
        collection.insert({image: read()}, function(err, docs) {
            console.log("data inserted");
            db.close();
        });
    });
}

function get() {
    var ret;
    MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
        if(err) throw err;

        var collection = db.collection('test_insert');

        collection.find().toArray(function(err, results) {
            //console.dir(results);
            // Let's close the db
            ret = results[0];
            db.close();
        });
    });
    return ret;
}

/*
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
    if(err) throw err;

    var collection = db.collection('test_insert');
    collection.insert({a: base64_data}, function(err, docs) {

        collection.count(function(err, count) {
            console.log(format("count = %s", count));
        });

        // Locate all the entries using find
        collection.find().toArray(function(err, results) {
            console.dir(results);
            // Let's close the db
            db.close();
        });
    });
});
*/

You have to send the image via XMLHttpRequest from the client to nodejs. 您必须通过XMLHttpRequest将图像从客户端发送到nodejs。 Then store it in mongo like you did with the file, you read by fs.readFileSync . 然后像使用fs.readFileSync读取文件一样,将其存储在mongo中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM