简体   繁体   English

AngularJS照片/文件上传

[英]AngularJS photo/file upload

Uploading file and photo seems to be working fine on other browsers apart from FF. 除了FF,在其他浏览器上上传文件和照片似乎效果很好。 It seems to be its failing somewhere in the form.append but I don't get why its working on Chrome and IE but not FF. 它似乎在form.append的某个地方失败了,但是我不明白为什么它可以在Chrome和IE上运行,但不能在FF上运行。

Can someone shed a light on this one please. 有人可以照亮这一点吗?

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);
                    })
            });
        }

Input: 输入:

<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)">

Error: 错误:

vm.profilePhoto is undefined
Profile/$scope.uploadProfilePhoto/

您不应该在上传功能中使用$ scope.vm吗?

I just notice that the vm object must be the self or this of the object/controller. 我只是注意到vm对象必须是对象或控制器的selfthis

And you need to declare the vm.profilePhoto object as well: 并且您还需要声明vm.profilePhoto对象:

 var vm = this; vm.profilePhoto = {}; 

JS: JS:

example here: 这里的例子:

https://jsfiddle.net/alvarojoao/v9rfn301/ 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);
        })
    });
  };

}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM