我正在尝试将base64中的图像上传到我的mongodb。用户将通过输入type =“ file”选择该图像,并且在全部编译完表单后,用户可以单击按钮“ SAVE”。我正在使用AngularJS,并且需要设置角度控制器,服务以及后端。
这就是我现在拥有的权利:
HTML:
<form class="form" name="form">
<div class="form-group">
<input type="text" class="form-control" ng-model="rooms.nome" id="nome"
required />
<label for="nome">Nome</label>
</div>
<div class="form-group">
<input type="file" ng-click="upload()">upload</button> //this is just an example
</div>
<div class="input-group-btn">
<button class="btn btn-primary" ng-click="saveRoom()" type="button"
ng-disabled="form.$invalid">Save</button>
</div>
</form>
角度控制器:
angular
.module('roomApp')
.controller('addRoomController', ['$scope', 'roomService', '$http', function ($scope, roomService, $http) {
$scope.saveRoom = function () {
$scope.room = [{
"nome": $scope.rooms.nome,
"posti": $scope.rooms.posti,
"indicazioni": $scope.rooms.indicazioni,
"indirizzo": {
"via": $scope.rooms.indirizzo.via,
"num": $scope.rooms.indirizzo.num,
"cap": $scope.rooms.indirizzo.cap,
"citta": $scope.rooms.indirizzo.citta,
"nazione": $scope.rooms.indirizzo.nazione,
"piano": $scope.rooms.indirizzo.piano
}
//I think here I have to manage the file
}];
$http({
method: 'POST',
url: '/irs/rooms/insert',
data: $scope.room,
}).success(function (data) {
console.log(data.ops[0])
roomService.setRoom(data.ops[0]);
location.href = "#/";
}).error(function (data) {
console.log('Error checking server');
});
}
}]);
Web客户服务:
angular
.module('roomApp')
.factory('roomService', ['$http', '$q', function ($http, $q) {
var rooms = [];
return {
checkRoom: function () {
return rooms;
},
setRoom: function (room) {
rooms.push(room);
},
};
}]);
DAL:
RoomsDal.prototype.defineModel = function(database) {
this.Rooms = database.model('Rooms', {
_id: {
type: 'objectId',
required: true,
unique: true,
index: true
},
nome: {
type: 'string',
required: true
},
posti: {
type: 'string',
required: true
},
indicazioni: {
type: 'string',
required: true
},
indirizzo : {
via: {
type: 'string',
required: true
},
num: {
type: 'string',
required: true
},
cap: {
type: 'string',
required: true
},
citta: {
type: 'string',
required: true
},
nazione: {
type: 'string',
required: true
},
piano: {
type: 'string',
required: true
},
image: {
data: 'buffer',
contentType: 'string' //I am not sure about this
}
}
});
};
RoomsDal.prototype.insertOrUpdate = function(id, nome, posti, indicazioni, indirizzo) {
return this.Rooms.findOneAndUpdateQ({
_id: id
}, {
_id: id,
nome: nome,
posti: posti,
indicazioni: indicazioni,
indirizzo: {
via: indirizzo.via,
num: indirizzo.num,
cap: indirizzo.cap,
citta: indirizzo.citta,
nazione: indirizzo.nazione,
piano: indirizzo.piano
}
//Here I have to insert the image
}, {
upsert: true,
'new': true
});
};
服务:
RoomsService.prototype.getDescriptor = function() {
return {
insertRooms: {
handler: this.insertRooms,
method: 'post',
path: '/irs/rooms/insert'
}
};
};
RoomsService.prototype.insertRooms = function(request, response, next) {
var Q = require('q');
Q.all(RoomsService.RoomsDal.insert(request.body))
.then(function(results){
response.send(results)
})
.fail(function(error) {
RoomsService.logger.error('Error while getting detail of service container. Error: ' + error.message);
RoomsService.prototype.sendGenericErrorResponse(response);
next();
});
};
有了这些信息,我该如何处理图像上传?感谢您的帮助