【问题标题】:Angularjs upload service with onLoad image?带有onLoad图像的Angularjs上传服务?
【发布时间】:2014-05-12 12:26:42
【问题描述】:

我对 onload image 和 angular-fie-upload 有一些问题。首先,我必须验证图像大小,然后我将使用以下代码将此图像上传到服务器端。我的服务是这样的:

 Services.factory('$uploadWrapper', ['$upload' , '$logger' , function ($upload, $logger) {
    return function (url, file, informations, onSuccess, onError, onProgress) {
        url = url || angular.noop;
        file = file || angular.noop;
        informations = informations || angular.noop;
        onSuccess = onSuccess || angular.noop;
        onError = onError || angular.noop;
        onProgress = onProgress || angular.noop;

        $upload.upload({
            url: url,
            method: 'POST',
            // headers: {'header-key': 'header-value'},
            // withCredentials: true,
            data: informations,
            file: file // or list of files: $files for html5 only
            /* set the file formData name ('Content-Desposition'). Default is 'file' */
            //fileFormDataName: myFile, //or a list of names for multiple files (html5).
            /* customize how data is added to formData. See #40#issuecomment-28612000 for sample code */
            //formDataAppender: function(formData, key, val){}
        }).progress(function (evt) {
            $logger.info(Math.min(100, parseInt(100.0 * evt.loaded / evt.total)));
            onProgress(evt);
        }).success(function (response) {
            $logger.info('POST' + url + angular.toJson(response))
            onSuccess(response);
        }).error(function (error) {
            $logger.error('POST' + url + ' ' + angular.toJson(error));
            onError(error);
        });
    }
}]);

对于验证过程,我将创建一个图像来获取图像的宽度和高度:

$scope.onFileSelect = function ($files) {
        $scope.loading = true;
        //$files: an array of files selected, each file has name, size, and type.
        file = $files[0];
        var img = new Image();
        img.src = _URL.createObjectURL(file);
        img.onload = function () {
            console.log(this.width + "x" + this.height);
            if (img.width > sizes.width && img.height > sizes.height) {
            $uploadWrapper(pinholeAdminServerRoutes.image.upload, file, {
                "operationType": 'channels',
                "objectId": $scope.channel.id,
                "size": 'large'
            }, function (response) {
                $scope.loading = false;
            }, function (error) {
                    $scope.errors.push(error);
                $scope.loading = false;
            });
            } else {
                $scope.imageSizeNotValid = true;
                $scope.loading = false;
            }
            console.log('finish loading');
        };
    };

但是,我的服务不能在 onload 块内工作。但同样的服务在没有 onload 块的情况下也能工作。

【问题讨论】:

    标签: image angularjs onload-event


    【解决方案1】:

    我终于在onload 事件中使用$scope.$apply 得到了解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多