如何在浏览器中本地保存拖动的图像

时间:2014-09-02 13:12:54

标签: javascript angularjs upload

我是新角色并在github https://github.com/danialfarid/angular-file-upload上使用angular-file-upload项目 在我的一个应用程序中。直到现在后端没有准备好,我希望删除的文件在本地浏览器中显示。有办法吗?

我的观点代码:

<div ng-controller="MyCtrl">
  <input type="file" ng-file-select="onFileSelect($files)">
  <div class="button" ng-file-select="onFileSelect($files)" data-multiple="true"></div>
  <div id="image_drop_box" ng-file-drop="onFileSelect($files)" ng-file-drag-over-class="optional- css-class-name-or-function" ng-show="dropSupported">drop files here</div>
  <div ng-file-drop-available="dropSupported=true" ng-show="!dropSupported">HTML5 Drop File is not supported!</div>
  <button ng-click="upload.abort()">Cancel Upload</button>
</div>

并且控制器中的逻辑如下所示:

.controller('MyCtrl', function($scope){
  $scope.onFileSelect = function($files) {
    //$files: an array of files selected, each file has name, size, and type.
    for (var i = 0; i < $files.length; i++) {
      var file = $files[i];
      $scope.upload = $upload.upload({
        //upload logic 
      });
    }
  };
});

1 个答案:

答案 0 :(得分:3)

Convert the image into Base64 code and after that put the Base64 code in the src of imageg tag.
And to convert Image into Base64 in angular I would recommend you to use 
[https://github.com/ninjatronic/angular-base64]

 After following instructions for using this library, you can simply call:

  var imageData=$base64.encode(image);
 Don't forget to inject in your module:

 .module('myApp', ['base64'])