上传文件和照片似乎在除 FF 之外的其他浏览器上运行良好。这似乎是它在 form.append 中的某个地方失败,但我不明白为什么它在 Chrome 和 IE 上工作,但在 FF 上不起作用。
有人可以解释一下这个吗?
Controller :
$scope.uploadProfilePhoto = function() {
$timeout(function(){
var form = new FormData();
form.append("fileName", vm.profilePhoto.fileName);
form.append('file', vm.profilePhoto.file);
ProfileService.uploadProfilePicture(form)
.then(function(response){
vm.ProfilePictureUrl = api.getQualifiedUrl('image/' + response.data.ImageId);
})
});
}
输入:
<label for="profilePhoto" class="photo-upd" >
<img data-ng-src="{{vm.ProfilePictureUrl}}" id="profile-picture_image" alt="Candidate profile photo" onchange="angular.element(this).scope().uploadProfilePhoto(this)" class="img-responsive">
<span><i class="fa fa-upload"></i> Upload Photo</span>
</label>
<input id="profilePhoto" type="file" name="profilePhoto" valid-file data-oh-file fileread="vm.profilePhoto.file" filename="vm.profilePhoto.fileName" class="hidden"onchange="angular.element(this).scope().uploadProfilePhoto(this)">
错误:
vm.profilePhoto is undefined
Profile/$scope.uploadProfilePhoto/
最佳答案
我只是注意到vm
对象必须是对象/ Controller 的self
或this
。
您还需要声明 vm.profilePhoto
对象:
var vm = this; vm.profilePhoto = {};
JS:
示例如下:
https://jsfiddle.net/alvarojoao/v9rfn301/
var app = angular.module("turingApp", []);
app.controller("turingController", ["$scope","$timeout", function($scope,$timeout) {
var vm = this;
vm.profilePhoto = {};
$scope.uploadProfilePhoto = function() {
$timeout(function() {
var form = new FormData();
form.append("fileName", vm.profilePhoto.fileName);
form.append('file', vm.profilePhoto.file);
ProfileService.uploadProfilePicture(form)
.then(function(response) {
vm.ProfilePictureUrl = api.getQualifiedUrl('image/' + response.data.ImageId);
})
});
};
}]);
关于javascript - AngularJS 照片/文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35317020/