我生成的&使用sails generate
命令實現了一個名爲User
的新API。當我嘗試上傳svg或jpg圖像時,它完美地工作,文件被上傳,用戶在數據庫中創建。但是,當我嘗試使用png文件時,保存到數據庫失敗!雖然png文件會按預期上傳,但用戶不會被保存。調試時,對象參數是空的,除了字段「公司」。當頭像是圖片時,在Sails.js中創建頭像的用戶失敗
生成所述API
$ sails generate api user
用戶控制器(API /控制器/ UserController.js)
'create': function (req, res, next) {
var params = req.params.all();
var format = req.param('format');
delete params['format'];
req.file('avatar').upload({
maxBytes: 10000000,
dirname: sails.config.appPath + '/assets/images/avatar/'
}, function (err, uploadedFiles) {
if (err) return res.negotiate(err);
console.log(uploadedFiles.length + ' file(s) uploaded successfully!');
console.log(uploadedFiles);
params["avatar"] = uploadedFiles[0].fd;
User.create(params, function userCreated(err, user) {
if (err) {
req.session.flash = {
err: err
}
return res.redirect("/user/new");
}
if (format == 'json') {
res.json(user);
} else {
res.redirect('user/show/' + user.id);
}
});
});
},
我的意見/用戶/ new.ejs:
<form id="register-form" action="/user/create" method="post" enctype="multipart/form-data">
<input id="icon_prefix" type="text" name="company" class="validate">
<input name="avatar" type="file">
<input class="file-path validate" type="text">
<input id="icon_prefix" type="text" name="first_name" class="validate">
<input id="icon_prefix" type="text" name="last_name" class="validate">
<input id="icon_telephone" type="tel" name="phone"
<input id="icon_mail" type="email" name="email" class="validate" required="" aria-required="true">
<input name="type" value="Auftraggeber" type="radio" id="auftraggeber" checked/>
<input name="type" value="Auftragnehmer" type="radio" id="auftragnehmer" disabled/>
<input id="password" type="password" name="password" pattern=".{6,}" title="6 characters minimum"
<input id="confirmation" type="password" class="validate">
<input type="hidden" value="<%= _csrf %>">
<input type="checkbox" id="accept"/>
<input type="checkbox" id="not_robot"/>
<button class="btn-large waves-effect waves-light" type="submit">Submit
</button>
</form>
用戶模型(API /模型/ user.js的)
module.exports = {
schema: true,
attributes: {
avatar: {
type: "String"
},
first_name: {
type: "String",
required: true
},
last_name: {
type: "String",
required: true
},
type: {
type: "String",
enum: ['Auftraggeber', 'Auftragnehmer']
},
phone: {
type: "String",
unique: true
},
email: {
type: "String",
email: true,
unique: true,
required: true
},
password: {
type: "String",
required: true
},
company: {
type: "String",
required: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
};
爲什麼這些downvotes? – Suisse