我尝试使用图片上传功能创建一个注册表单,因此我通过帖子和enctype =" multipart / form-data"来获取ejs端的值。 / p>
<form method="post" action= "/SignUp" enctype="multipart/form-data" >
<div class="form-group">
<label for="firstName">First name</label>
<input type="text" name="firstName" id="firstName" class="form-control" value="<%= locals.firstName || '' %>" required />
</div>
<div class="form-group">
<label for="lastName">Last name</label>
<input type="text" name="lastName" id="lastName" class="form-control" value="<%= locals.lastName || '' %>" required />
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" value="<%= locals.username || '' %>" required />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" required />
</div>
<div class = "from-group">
<label for = "Image">Image</label>
<input Content-Type = "multipart/form-data" type ="file" name = "Image" id = "Image" class = "form-control" required/>
</div
<br />
<br />
<div class="form-group">
<button type="submit" class="btn btn-primary">Register</button>
<a href="/login" class="btn btn-link">Cancel</a>
</div>
</form>
我使用busboy从服务器端处理它
SignUp:function(req,res){
let reg = new Registrations();
var busboy = new Busboy({
headers: req.headers,
limits: {
fileSize: 6*1024*1024 //2MB limit
}
});
var stream;
var fstream;
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
if(fieldname == 'firstName')
reg.firstName = val;
else if (fieldname == 'lastName')
reg.lastName = val;
else if(fieldname == 'username')
reg.username = val;
else {
reg.password = val;
}
})
busboy.on('file', function(fieldname,file, filename,encoding,mimeType){
stream = __dirname + '/img/' + filename;
fstream = fs.createWriteStream(__dirname + '/img/' + filename);
file.pipe(fstream);
fstream.on('close', function(){
reg.Image = stream;
reg.save(function(err,reg){
if(err){
res.send(err.message)
console.log(err);
}else{
console.log(reg);
}
})
})
})
busboy.on('finish', function() {
})
res.render('login');
}
每次尝试时都会显示此错误 TypeError:无法读取&#39; on&#39;未定义的行
req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType)
你能告诉我这个问题是什么吗?
答案 0 :(得分:0)
在第一次看到busboy-doc之后,在我看来,因为busboy不是中间件,它正在扩展请求,请参阅代码片段:
var busboy = new Busboy({ headers: req.headers });
busboy.on('file',...
答案 1 :(得分:0)
我之前使用 busboy 上传图片,我将与您分享配置及其完成方式。
导入busboy模块
var Busboy = require('busboy');
然后在帖子端点内部声明busboy对象
var busboy = new Busboy({
headers: req.headers,
limits: {
fileSize: 6*1024*1024 //2MB limit
}
});
其次是后端点
中的其余代码busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
//extract intput-field from upload-form
});
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
//process each file upload, check for size, mimetype,
//store if all checks passed, delete otherwise, move to
//next file
});
//finish call back when all files are uploaded
busboy.on('finish', function() {
//do cleanup and other related work
});
return req.pipe(busboy);
我已经在项目中创建了一个要点,我使用 busboy 上传多张图片并进行以下检查
答案 2 :(得分:0)
我使用了multer而且工作正常