簡體   English   中英

AngularJS照片/文件上傳

[英]AngularJS photo/file upload

除了FF,在其他瀏覽器上上傳文件和照片似乎效果很好。 它似乎在form.append的某個地方失敗了,但是我不明白為什么它可以在Chrome和IE上運行,但不能在FF上運行。

有人可以照亮這一點嗎?

控制器:

$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/

您不應該在上傳功能中使用$ scope.vm嗎?

我只是注意到vm對象必須是對象或控制器的selfthis

並且您還需要聲明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);
        })
    });
  };

}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM