gpt4 book ai didi

node.js - 捕获字符串并将其存储在 mongoDB 中

转载 作者:可可西里 更新时间:2023-11-01 09:06:19 26 4
gpt4 key购买 nike

我正在尝试为用户上传图片时捕获文本生成的字符串(基于客户端/ session )。

上传时从控制台执行 db.collection.find(); 时的输出:

"_id" : ObjectId("590c67f472667e031fe80a9d"),
"path" : "uploads/bicycle.jpg",
"originalname" : "bicycle.jpg",
"__v" : 0

这里我也想要 "imagelocation": "N/A"

该字符串基于用户上传图片时的位置。我想将该特定字符串值连接到上面显示的图像对象 ID。

应用程序.js:

/image UPLOAD TO MONGODB

var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var path = require('path');
app.use(bodyParser.json());

//To get the access for the functions defined in imagefile.js class
var routes = require('./imagefile');

// connect to mongo,
mongoose.connect('mongodb://localhost:27017/gps');

app.use('/', routes);

// To get all the images/files stored in MongoDB
app.get('/images', function(req, res) {
routes.getImages(function(err, genres) {
if (err) {
throw err;
}
res.json(genres);

});
});

app.get('/images/:id', function(req, res) {
routes.getImageById(req.params.id, function(err, genres) {
if (err) {
throw err;
}

res.send(genres.path)
});
});

path 和 originalname 在我的 imagefile.js 中声明如下:

var imageSchema = mongoose.Schema({
path: {
type: String,
required: true,
trim: true
},
originalname: {
type: String,
required: true
},
imagelocation:{ // format for storing
type: String,
required: true
}

});
module.exports = mongoose.model('Image', stringClass);
var Image = module.exports = mongoose.model('files', stringClass);

router.getImages = function(callback, limit) {

Image.find(callback).limit(limit);
}


router.getImageById = function(id, callback) {

Image.findById(id, callback);

}

router.addImage = function(image, callback) {
Image.create(image, callback);
}
//multer
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/')
},
filename: function(req, file, cb) {
cb(null, file.originalname);
},
imagelocation: function(req,file,cb){
cb(null, $('#coordinates').innerHTML);
}
});

var upload = multer({
storage: storage
});

router.get('/', function(req, res, next) {
res.render('layouts/main.handlebars');
});

router.post('/', upload.any(), function(req, res, next) {

res.send(req.files);

var path = req.files[0].path;
var imageName = req.files[0].originalname;
var imagepath = {};
imagepath['path'] = path;
imagepath['originalname'] = imageName;


router.addImage(imagepath, function(err) {

});

});

module.exports = router;

HTML:

<p id="coordinates">String is generated here</p>

TL;DR - 在将其上传到我的 MongoDB 时,我如何捕获字符串并将其与图像一起发送?

最佳答案

要在图像文件中发送一个长字符串,只需在提交时将其与表单一起发送即可。例如在隐藏的输入字段中。

然后,在提交时,您可以将其作为 req.body 对象的一部分进行访问

这是一个示例(来自 API,但您明白了):

app.post('/api/file', function(req, res){
var upload = multer({
storage: storage
}).single('imageFile')
upload(req, res, function(err){
if(err){
// handle any errors here ...
}
console.log(req.body)// <-- here you can access your text string as 'req.body.yourStringIdentifier'
res.json({success: true, msg: 'File uploaded'});
})
})

如果您有任何问题,请随时提问:)


编辑:完整示例

服务器.js:

const express = require('express');
const multer = require('multer');
const path = require('path');
const ejs = require('ejs');
var app = express();
app.set('view engine','ejs');

app.get('/', function(req, res){
res.render('index');
})

var storage = multer.diskStorage({
destination: function(req, file, callback){
callback(null, './uploads');
},
filename: function(req, file, callback){
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
})

app.post('/', function(req, res){
var upload = multer({
storage: storage
}).single('imageFile')
upload(req, res, function(err){
console.log(req.body.textInput);
res.end('File is uploaded');
})
})

var port = process.env.PORT || 7850
app.listen(port, function(){
console.log('Listening on port ' + port);
})

index.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>INDEX</title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data" method="post">
<input type="file" name="imageFile">
<input type="text" name="textInput">
<input type="submit" value="Upload file" name="submit">
</form>
</body>
</html>

当我使用 nodemon server.js 从终端运行这段代码时,它会起作用。提交表单时,文本字段的内容会打印到控制台。

关于node.js - 捕获字符串并将其存储在 mongoDB 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com