我正在创建一个带有表单的简单页面,用于将图像和其他数据上传到Sql Server数据库。我的服务器是用Node.js构建的,我正在使用Sequelize来连接数据库,而我正在使用multer来获取req.files。
我是陌生人,所以我可能错过了一些明显的东西,但是我试图从multer上传三张图片,从req.files中的文件对象中读取缓冲区,使用.toString( )在这些缓冲区上,然后使用Sequelize将这些基本64字符串写入Sql Server数据库。问题是它只为req.files数组中的最后一项写入字符串。它是哪一个并不重要,但它只写最后一个从Node.js到控制台的输出说它正在将所有三个字符串写入数据库,但当你查看表时,没有值柱。我不确定我做错了什么,非常感谢任何帮助。我将粘贴下面的代码,如果有更多信息可以帮助我,请告诉我。
先谢谢你的帮助!
这是我的路线:
'use strict';
var Event = require('../models/Event');
var fs = require('fs');
var clc = require('cli-color');
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
var bodyparser = require('body-parser');
var path = require('path');
var Sql = require('sequelize');
var sql = new Sql('events_page', 'eventsUser', 'p@ssw0rd1', {
host: 'localhost',
dialect: 'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
module.exports = function (router) {
router.route('/createevent')
.post(upload.array('images', 3), function (req, res, next) {
sql.sync()
.then(function () {
Event.create(req.body)
.then(function (newEvent) {
newEvent.update({
eventHeaderImage: req.files[0].buffer.toString('base64'),
eventBackgroundImage: req.files[1].buffer.toString('base64'),
eventSliderImage: req.files[2].buffer.toString('base64')
})
.then(function (eventWithPics) {
res.redirect('/admin');
});
});
});
});
};
这是我的服务器:
'use strict';
var express = require('express');
var app = express();
var clc = require('cli-color');
var port = process.env.PORT || 3000;
var time = new Date();
var dbRouter = express.Router();
var adminRouter = express.Router();
require('./routes/db-routes')(dbRouter);
require('./routes/admin-routes')(adminRouter);
app.use(express.static(__dirname + '/'));
app.use('/', dbRouter);
app.use('/', adminRouter);
app.listen(port, function () {
console.log(clc.cyanBright('server started on port ' + port + ' at ' + time));
});
这是我的事件表的架构:
'use strict';
var fs = require('fs');
var path = require('path');
var Sql = require('sequelize');
var sql = new Sql('events_page', 'eventsUser', 'p@ssw0rd1', {
host: 'localhost',
dialect: 'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
var Event = module.exports = sql.define('Event', {
eventName: Sql.STRING,
eventLocation: Sql.STRING,
eventStartDate: Sql.DATE, //the start date...
eventEndDate: Sql.DATE, // the end date...
eventHeaderImage: Sql.TEXT, //bas64 string of header image
eventBackgroundImage: Sql.TEXT, //image to appear on event slide on homepage
eventSlideUpText: Sql.TEXT, //slide up text for future events page
sponsorsHeading: Sql.TEXT, //heading for sponsors section
sponsorsParagraph: Sql.TEXT, // paragraph below heading on sponsors section
eventSliderImage: Sql.TEXT, //image for front page slider
homepageBulletOne: Sql.STRING,
homepageBulletTwo: Sql.STRING,
homepageBulletThree: Sql.STRING
},
{
getterMethods : {
eventUrl: function () {
return this.eventName.replace(/\W/g, '').toLowerCase() + '-' + this.eventStartDate.getFullYear();
}
}
});
Event.sync({force: false});
最后这是HTML:
<form action="/createevent" id="createEventForm" method="post" enctype="multipart/form-data">
<label class="col_4" for="newEventName">Event Name</label>
<input class="col_8" id="newEventName" name="eventName" type="text submit" placeholder="Event Name" />
<label class="col_4" for="newEventLocation">Event Location</label>
<input class="col_8" id="newEventLocation" name="eventLocation" type="text submit" placeholder="Event Location" />
<label class="col_4" for="newEventStartDate">Start Date</label>
<input class="col_8" id="newEventStartDate" name="eventStartDate" type="date" placeholder="Event Start Date" />
<label class="col_4" for="newEventEndDate">End Date</label>
<input class="col_8" id="newEventEndDate" name="eventEndDate" type="date" placeholder="Event End Date" />
<label class="col_4" for="newEventHeaderImage">Header Image</label>
<input class="col_8" id="newEventHeaderImage" name="images" type="file" placeholder="Header Image" />
<label class="col_4" for="newEventBackgroundImage">Future Page Image</label>
<input class="col_8" id="newEventBackgroundImage" name="images" type="file" placeholder="Image for Future Events Page" />
<label class="col_4" for="newEventSlideUpText">Future Page Text</label>
<input class="col_8" id="newEventSlideUpText" name="eventSlideUpText" type="text submit" placeholder="Text for Future Events Page" />
<label class="col_4" for="newSponsorsHeading">Sponsor Tab Heading</label>
<input class="col_8" id="newSponsorsHeading" name="sponsorsHeading" type="text submit" placeholder="Heading for Sponsor Tab" />
<label class="col_4" for="newSponsorsParagraph">Sponsor Tab Intro Paragraph</label>
<input class="col_8" id="newSponsorsParagraph" name="sponsorsParagraph" type="text submit" placeholder="Intro Paragraph for Sponsor Tab" />
<label class="col_4" for="newEventSliderImage">Image for Homepage slider</label>
<input class="col_8" id="newEventSliderImage" name="images" type="file" placeholder="Image for Homepage slider" />
<label class="col_4" for="newHomepageBulletOne">Homepage Bullet One</label>
<input class="col_8" id="newHomepageBulletOne" name="homepageBulletOne" type="text submit" placeholder="Heading for Sponsor Tab" />
<label class="col_4" for="newHomepageBulletTwo">Homepage Bullet Two</label>
<input class="col_8" id="newHomepageBulletTwo" name="homepageBulletTwo" type="text submit" placeholder="Heading for Sponsor Tab" />
<label class="col_4" for="newHomepageBulletThree">Homepage Bullet Three</label>
<input class="col_8" id="newHomepageBulletThree" name="homepageBulletThree" type="text submit" placeholder="Heading for Sponsor Tab" />
<br />
<br />
<button class="medium" id="createEventButton" type="submit">Create Event</button>
</form>
答案 0 :(得分:0)
在您的路线文件中,您有:
.post(upload.array('images', 3)
这意味着您可以通过表单发送一个输入文件字段,用户最多可以选择三个文件。但是你在HTML中创建了三个相等的输入文件字段。因此,只有一个发送到您的服务器。
在HTML表单中输入不同的输入文件,例如:
...
<input class="col_8" id="newEventHeaderImage" name="headerImage" type="file" placeholder="Header Image" />
...
<input class="col_8" id="newEventBackgroundImage" name="backgroundImage" type="file" placeholder="Image for Future Events Page" />
...
<input class="col_8" id="newEventSliderImage" name="sliderImage" type="file" placeholder="Image for Homepage slider" />
在您的路线档案中:
...
.post(upload.fields([{ name: 'headerImage', maxCount: 1 }, { name: 'backgroundImage', maxCount: 1 }, { name: 'sliderImage', maxCount: 1 }]), function (req, res, next) {
sql.sync()
.then(function () {
Event.create(req.body)
.then(function (newEvent) {
newEvent.update({
eventHeaderImage: req.files['headerImage'][0].buffer.toString('base64'),
eventBackgroundImage: req.files['backgroundImage'][0].buffer.toString('base64'),
eventSliderImage: req.files['sliderImage'][0].buffer.toString('base64')
})
...
我没有运行此代码,这是概念
答案 1 :(得分:0)
事实证明字符串太长了。当我开始存储文件名并使用链接而不是base64字符串获取图像时,它就起作用了。
感谢您的帮助!