我正在尝试将base64与节点rest api端点一起使用。这是我来自路由器文件product.js的代码,其中我为文件和其他数据定义了中间件:
public List<EngMwoReschedulingTxn> AddMwoReschedule(DateChange dateChange)
{
DateTime dt = new DateTime();
dt = dateChange.RescheduleDate;
conn.Query<DataTable>("update DataTable set TargetDateTime='" + dt + "' where WorkOrderId='"+engMwoReschedulingTxn.WorkOrderId+"'");
}
我有一个名为“ uploads”的空文件夹,这是 app.js的代码:
router.post("/", upload.any('productImage'), (req, res, next) => {
var fileInfo = [];
for (var i = 0; i < req.files.length; i++) {
fileInfo.push({
"originalName": req.files[i].originalName,
"size": req.files[i].size,
"b64": new Buffer(fs.readFileSync(req.files[i].filename)).toString("base64")
});
fs.unlink(req.files[i].path);
}
res.send(fileInfo);
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price,
// productImage: req.file.filename,
reference: req.body.reference,
description: req.body.description,
quantity: req.body.quantity,
date: req.body.date,
category: req.body.category,
newProduct: req.body.newProduct,
relatedProduct: req.body.relatedProduct,
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Created product successfully",
createdProduct: {
name: result.name,
price: result.price,
_id: result._id,
request: {
type: 'GET',
url: "https://booksbackend.herokuapp.com/products/" + result._id
}
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
我不确定代码是否正确。通过邮递员出错:
var fs = require("fs");
var multer = require("multer");
const productRoutes = require("./api/routes/products");
const userRoutes = require("./api/routes/user");
mongoose.connect(
"mongodb://****:*****@ds141661.mlab.com:41661/books-database", {
useMongoClient: true
}
);
mongoose.Promise = global.Promise;
app.use(morgan("dev"));
app.use('/uploads', express.static('uploads'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
我是使用base64的新手,请帮忙。