【发布时间】:2015-09-23 09:04:27
【问题描述】:
我有点困惑如何使用基本形式的角度访问文件数据。我正在关注:(https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs) 和 youtube (https://www.youtube.com/watch?v=vLHgpOG1cW4)。他们似乎做对了,但是当我尝试时,事情似乎有所不同。无论如何,这是我的 HTML 表单:
<form>
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" ng-model="customer.name" id="name" class="form-control"/>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" ng-model="customer.email" id="name" class="form-control"/>
</div>
<div class="form-group">
<label for="file">Image</label>
<input type="file" file-model="customer.file" id="file" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" ng-click="submit()" class="btn btn-primary">Submit</button>
</div>
</form>
还有指令
app.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
最后是控制器:
app.controller('CustomerController', ['$scope','CustomerService', function($scope, CustomerService){
$scope.customer = {};
CustomerService.save($scope.customer);
}]);
当我在我看来{{ customer }} 时,我会得到这样的结果:
{"name":"Robert DeNiro","email":"robie@godfather.com","file":{}}
最后一个空的 "file":{} 文件对象导致我无法将值发布到服务器。
这是我的客户服务代码:
var CustomerService = function($resource) {
return $resource('advertisements/:id', { id: '@_id' }, {
update: {
method: 'PUT' // this method issues a PUT request
},
save: {
method: 'post',
transformRequest: angular.identity,
'Content-Type': undefined
}
});
};
CustomerService.$inject = ['$resource'];
app.service('CustomerService', CustomerService);
我正在使用 Laravel 5.1,它在所有字段上报告验证错误“必需”,这表明发送了一个空对象。提前致谢。
【问题讨论】:
标签: php angularjs file-upload angularjs-directive laravel-5