你如何使用 ng-file-upload 和 ngImgCrop?

How do you use ng-file-upload and ngImgCrop?

我想创建一个文件上传器,我可以将图片上传到我的 UI,裁剪它,然后将它发送到我的服务器。我一直在使用 ngFileUpload 上传文件,效果很好。我按照说明将 ngImgCrop 添加到我的项目中,并将 html 和 css 从 http://jsfiddle.net/danialfarid/xxo3sk41/590/ 导入到我的项目中以弄清楚如何使用它。乍一看一切都很好,但是当我 select 从我的硬盘驱动器中拍摄一张图片时,它没有出现在裁剪区域中。可能隐藏了某些东西还是有其他问题?这是 jsfiddle html 和 css:

<style>
    .cropArea {
      background: #E4E4E4;
      overflow: hidden;
      width:500px;
      height:350px;
    }
  </style>

<div>Crop Image and Upload</div>
        <button ngf-select ng-model="picFile" accept="image/*">
            Select Picture</button>
        <div ngf-drop ng-model="picFile" ngf-pattern="image/*"
             class="cropArea">
            <img-crop image="picFile  | ngfDataUrl"                 
            result-image="croppedDataUrl" ng-init="croppedDataUrl=''">
            </img-crop>
        </div>
        <div>
            <img ng-src="{{croppedDataUrl}}" />
        </div>
        <button ng-click="upload(croppedDataUrl, picFile.name)">Submit</button> 

我有仅适用于上传的代码。图片按预期显示。我想知道为什么它是为一个而不是另一个。此代码适用于 ng-file-upload:

<div class="photo-upload-text">Upload your image</div>
<a class="upload-button" ngf-select ng-model="logo" href="#">Choose File&nbsp;</a>
<img ngf-src="logo" ngf-default-src="'/thumb.jpg'" ngf-accept="'image/*'">

最后,我举了两个例子。这个允许我裁剪图片,但它不会生成可以轻松上传的没有前缀的 base64 文件。第二张虽然类似,但没有将图片进行裁剪。

允许裁剪但不生成可接受的 base64 文件

<div>Select an image file: <input type="file" id="fileInput" /></div>
<div class="cropArea">
  <img-crop image="myImage" result-image="myCroppedImage"></img-crop>
</div>
<div>Cropped Image:</div>
<div>
  <img ng-src="{{myCroppedImage}}" />
</div>

我认为这提供了一个很好的 base64 文件,但我无法裁剪它以找出答案

<div>Crop Image and Upload</div>
<button ngf-select ng-model="picFile" accept="image/*">Select Picture</button>
<div ngf-drop ng-model="picFile" ngf-pattern="image/*"class="cropArea">
    <img-crop image="picFile  | ngfDataUrl" result-image="croppedDataUrl" ng-init="croppedDataUrl=''"></img-crop>
</div>
<div>Cropped Image:</div>
<div>
    <img ng-src="{{croppedDataUrl}}" />
</div>

Everything looks good at first glance but when I select a picture from my hard drive it does not appear in the crop area.

当我尝试使用下面的代码重现您的问题时,一切正常。

angular.module('app', ['ngFileUpload', 'ngImgCrop']).controller('UploadController', function($scope) {

});
    .cropArea {
      background: #E4E4E4;
      overflow: hidden;
      width:500px;
      height:350px;
    }
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/ng-img-crop/0.3.2/ng-img-crop.css" rel="stylesheet"/>
</head>

<body ng-app="app">

  <div>Crop Image and Upload</div>
<button ngf-select ng-model="picFile" accept="image/*">
  Select Picture</button>
<div ngf-drop ng-model="picFile" ngf-pattern="image/*"
     class="cropArea">
  <img-crop image="picFile  | ngfDataUrl"                 
            result-image="croppedDataUrl" ng-init="croppedDataUrl=''">
  </img-crop>
</div>
<div>
  <img ng-src="{{croppedDataUrl}}" />
</div>
<button ng-click="upload(croppedDataUrl, picFile.name)">Submit</button> 
  
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.0.4/ng-file-upload.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-img-crop/0.3.2/ng-img-crop.js"></script>
 </body>

我尝试了 ngImgCrop 和 ng-file-upload 的例子,但它对我不起作用。我需要添加以下代码才能使其工作:

    var app = angular.module('fileUpload', ['ngFileUpload', 'ngImgCrop']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.upload = function (dataUrl, name) {
        Upload.upload({
            url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
            data: {
                file: Upload.dataUrltoBlob(dataUrl, name)
            },
        }).then(function (response) {
            $timeout(function () {
                $scope.result = response.data;
            });
        }, function (response) {
            if (response.status > 0) $scope.errorMsg = response.status 
                + ': ' + response.data;
        }, function (evt) {
            $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
        });
    }
}]);

此代码:

                $scope.myImage='';
            $scope.myCroppedImage='';

            var handleFileSelect=function(evt) {
                var file=evt.currentTarget.files[0];
                var reader = new FileReader();
                reader.onload = function (evt) {
                    $scope.$apply(function($scope){
                        $scope.myImage=evt.target.result;
                    });
                };
                reader.readAsDataURL(file);
            };
            angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);