【问题标题】:Angular upload file directive角度上传文件指令
【发布时间】:2015-11-20 03:42:17
【问题描述】:

在我的 Angular 应用程序中,我需要一个用于上传文件的指令(选择文件,并将其作为二进制数据获取)。

我用了很多次,所以它必须是通用的。

最好的方法是什么?

【问题讨论】:

    标签: angularjs file-upload angularjs-directive


    【解决方案1】:

    https://github.com/flowjs/ng-flow

    这很好用。当然可以在你身边制作一些包装指令。

    【讨论】:

      【解决方案2】:

      指令代码

       .directive("fileReaderGallery", ['background', function (background) {
        return {
          restrict: "E",
          scope: true,
          templateUrl: "templates/directive.html",
          replace: true,
          link: function ($scope, el, attrs) {
              var input = null,
                  drag_image_gallery = el.find('.drag_image_gallery');
      
              $scope.dragging = false;
              $scope.fileselected = false;
              $scope.uploaded = false;
              $scope.uploading = false;
              $scope.image = null;
      
              $scope.clearFileReader = function () {
                  if (!attrs.styling || !input) return false;
      
                  $scope.formTitan.elementStyles[attrs.styling][$scope.styledItem.pt]['background-image'] = '';
      
                  $scope.formSelected[attrs.styling].imageFile = null;
                  $scope.formSelected[attrs.styling].isImage = false;
      
                  input.value = '';
                  $scope.fileselected = false;
                  $scope.imageName = '';
              };
              var readfiles = function (files) {
                  var reader, bg;
                  if (files && files[0]) {
                      if (files.length > 1) {
                          return console.log("Select single file only");
                      }
      
                      reader = new FileReader;
                      reader.onload = function (e) {
                          if (files[0].type.indexOf('image/') !== -1) {
                              if (e.target.result) {
                                  bg = {
                                      'background-image':  e.target.result,
                                      'background-repeat': 'repeat',
                                      'background-position': 'top',
                                      'background-size': ''
                                  };
                                  $scope.uploading = true;
                                  $scope.$apply(function () {
                                      background.add(angular.copy(bg));
                                      $scope.current.dcBGImage = angular.copy(bg);
                                      $scope.imageName = files[0].name;
                                      $scope.image = e.target.result;
                                      $scope.fileselected = true;
                                      console.log(files[0])
                                  });
                              }
                          } else {
                              return console.log('Please select an Image');
                          }
                      };
                      return reader.readAsDataURL(files[0]);
                  }
              };
      
              $scope.clickUpload = function () {
                  el.find('.bg-file-reader').click();
              };
      
              drag_image_gallery[0].ondragover = function () {
                  $scope.dragging = true;
                  //drag_image_gallery.addClass('ondragover');
                  $scope.$digest();
                  return false;
              };
      
              drag_image_gallery[0].ondragleave = function () {
                  $scope.dragging = false;
                  $scope.$digest();
                  //drag_image_gallery.removeClass('ondragover');
              };
      
              drag_image_gallery[0].ondrop = function (e) {
                  $scope.dragging = false;
                  $scope.$digest();
                  //drag_image_gallery.removeClass('ondragover');
                  e.preventDefault();
                  readfiles(e.dataTransfer.files);
              };
      
              el.find('.bg-file-reader').on('change', function () {
                  readfiles(this.files);
              });
          }
      };
      }]);
      

      html模板代码

      <div class="row upload_image text-center">
          <div class="drag_image drag_image_gallery row text-center" ng-class="        {ondragover:dragging}">
          Drag an Image here
        </div>
        OR
       <div class="row text-center choose_computer">
           <button ng-click="clickUpload()" class="btn btn-default">Choose from     your computer</button>
            <input type="file" class="bg-file-reader upload" name="gallery"/>
        </div>
      </div>
      

      具有拖放和选择文件两种功能的指令

      【讨论】:

      • 指令中的“背景”是什么?它会导致错误!
      • 删除多余的内容
      • 只是我用来实时更改背景图像的基本功能
      • 你要什么我帮你做
      猜你喜欢
      • 2018-06-04
      • 2017-04-01
      • 1970-01-01
      • 2015-06-28
      • 2023-03-14
      • 2016-05-05
      • 2017-12-30
      • 2020-12-02
      • 1970-01-01
      相关资源
      最近更新 更多