我有一个输入文件
<div class="form-group">
<div class="input-group input-file">
<span class="input-group-btn">
<button class="btn btn-default btn-choose" type="button">Choose</button>
</span>
<input type="text" class="form-control" id="img" name="img" placeholder='Choose a file...' />
<span class="input-group-btn">
<button class="btn btn-warning btn-reset" type="button">Reset</button>
</span>
</div>
</div>
我想将图像上传到mongodb,但是我需要为其提供完整的路径(即C:/Users/MyPc/Desktop/217680967585220926475.jpg到本地客户端计算机上的图像。) JS代码获取格式为blob:null / b5ae00f1-2a78-48ab-b03c-560c51184af4的图像的临时存储位置,但是mongodb无法理解。我该怎么办?
$(".input-file").before(
function() {
if (!$(this).prev().hasClass('input-ghost')) {
var element = $("<input type='file' class='input-ghost' style='visibility:hidden; height:0'>");
element.attr("name", $(this).attr("name"));
element.change(function (event) {
//get temporary part for node
var tempPath = URL.createObjectURL(event.target.files[0]);
console.log(tempPath);
element.next(element).find('input').val(tempPath);
});
}
});
后端是这张图片中的格式
相册的模型
const mongoose = require('mongoose');
const Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
//{type: Date, required: true}
let PhotoAlbumSchema = new Schema({
title: String,
img: { _type: String, data: Buffer },
imgName: String,
albums:[
{
u_name: String,
u_title: String
}]
}
,{
timestamps: true
});
PhotoAlbumSchema.virtual('pictureId').get(function(){
return this._id;
});
// Export the model
module.exports = mongoose.model('PhotoAlbum', PhotoAlbumSchema);
photoalbum.controller
const PhotoAlbum = require('../models/photoalbum.model');
var fs = require('fs');
var path = require('path');
let counter = 0;
exports.photoalbum_create = function (req, res, next) {
console.log(req.body.img.data);
var imageData = fs.readFileSync( req.body.img.data);
console.log(imageData)
// delete req.body._id;
let photoalbum = new PhotoAlbum(
{
title: req.body.title,
img: {_type: 'image/png', data: imageData},
imgName: "gallery" + counter,
albums: {u_name: req.body.albums.u_name, u_title: req.body.albums.u_title}
}
);
photoalbum.save().then(img => {
console.log("Saved an image 'jsa-header.png' to MongoDB.");
// Find the stored image in MongoDB, then save it in '/static/assets/tmp' folder
PhotoAlbum.findById(img, function (err, photoalbum) {
if (err) throw err;
try{
fs.writeFileSync(path.join(__dirname, '../Views/assets/images/gallery' + counter + '.png'), photoalbum.img.data);
console.log("Stored an gallery" + counter + ".png in '../Views/assets/images' folder.");
counter++;
// // exit node.js app
// console.log("Exit!");
// process.exit(0);
}catch(e){
console.log(e);
}
})
res.send('PhotoAlbum Created successfully')
}).catch(err => {
console.log(err);
throw err;
});
};
关于相册的路线
const express = require('express');
const router = express.Router();
//
// Require the controllers
const photoalbum_controller = require('../controllers/photoalbum.controller');
//
router.post('/create', photoalbum_controller.photoalbum_create);
// a simple test url to check that all of our files are communicating correctly.
router.get('/test', photoalbum_controller.test);
module.exports = router;
不一样。所有方法都无法解决我的问题,我还没有找到如何将图像完整路径传递给nodesjs fileread,或将缓冲区从客户端发送到mongodb