我想要使用圖像上傳能力註冊表單,所以我採取在ejs一側使用這個帖子和enctype =「multipart/form-data」的值上傳圖像使用busboy meanstack undefined
<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>
,我使用打雜
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');
}
處理它從服務器端每一次我嘗試它表明我這個錯誤是
類型錯誤:無法讀取「開」的未定義的屬性就行了
req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType)
你能告訴我這是什麼問題嗎?
現在它不顯示錯誤了,但這些字段沒有收到,我可以」 t將它們保存在貓鼬 –