我的Meanjs应用程序应该在我的UserSchema中将徽标上传到嵌套架构中。我能够成功上传文件,因为我能够成功地为用户存储个人资料照片。但是当我试图将图像保存到嵌套的Schema中时,它会失败。
我的模型看起来像这样:
ClubSchema = {name:String,
logo:Buffer};
UserSchema = {name:String,
photo:Buffer,
club:[ClubSchema]
}
我能够将所有数据成功插入我的ClubSchema,但不能将徽标插入。
我的client.controller.js
var user = new Users($scope.user);
console.dir(user.club); //I can see the logo here
for(var i = 0;i<user.club.length;i++){
user.club[i].logo = $scope.uploadedLogos[i]; //Still trying to force it into the object
user.club[i].hasLogo = true; //Mysterious hasLogo = false appears
}
user.$update(function(response) {
$scope.success = true;
console.log('Inside scope success User is ');
console.dir(user); //hasLogo=false appears in this and no logo
$location.path('/');
}, function(response) {
$scope.error = response.data.message;
});
我的server.controller.js
if (user) {
// Merge existing user
user = _.extend(user, req.body);
user.updated = Date.now();
user.displayName = user.name;
user.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.status(400).send(err);
} else {
res.json(user);
}
});
}
});
} else {
res.status(400).send({
message: 'User is not signed in'
});
}
hasLogo字段完全令人困惑,因为我的代码中不存在这样的赋值。我添加了hasLogo = true,希望以某种方式将hasLogo设置为true然后上传图像,但这也不起作用。我不知道这个标志来自哪里。在hasLogo = true循环之后,如果我打印对象,我可以看到两个hasLogo字段,一个是true而另一个是false。 $ update成功后,它总是错误的。我的模型不包含hasLogo字段。
答案 0 :(得分:0)
这是我使用 MEAN.JS 进行文件上传的方式。
<强>模型强>
var UserSchema = new mongoose.Schema({
name:{type:String,required:true},
photo:Buffer // Image
});
服务器控制器
var userPicture = function(req,res){ // Stores Picture for a user matching the ID.
user.findById(req.param('id'), function (err, user) {
console.log(req.files) // File from Client
if(req.files.file){ // If the Image exists
var fs = require('node-fs');
fs.readFile(req.files.file.path, function (dataErr, data) {
if(data) {
user.photo ='';
user.photo = data; // Assigns the image to the path.
user.save(function (saveerr, saveuser) {
if (saveerr) {
throw saveerr;
}
res.json(HttpStatus.OK, saveuser);
});
}
});
return
}
res.json(HttpStatus.BAD_REQUEST,{error:"Error in file upload"});
});
};
客户端控制器
$scope.saveuserImage = function(){
$scope.upload = $upload.upload({ // Using $upload
url: '/user/'+$stateParams.id+'/userImage', // Direct Server Call.
method:'put',
data:'', // Where the image is going to be set.
file: $scope.file
}).progress(function (evt) {})
.success(function () {
var logo = new FileReader(); // FileReader.
$scope.onAttachmentSelect = function(file){
logo.onload = function (e) {
$scope.image = e.target.result; // Assigns the image on the $scope variable.
$scope.logoName = file[0].name; // Assigns the file name.
$scope.$apply();
};
logo.readAsDataURL(file[0]);
$scope.file = file[0];
$scope.getFileData = file[0].name
};
location.reload();
$scope.file = "";
$scope.hideUpload = 'true'
});
$scope.getFileData = '';
// location.reload()
};
<强> HTML 强>
<input ng-file-select="onAttachmentSelect($files)" ng-model="getFileData" name="upload" type="file" required="true">
ng-file-select 用于从客户端获取文件。
这对我来说很好。希望这可以帮助。