0

I have an example for drag and drop image in angularjs. If I drag and drop the image it is displying on down. I want to drop the image inside the box itself. How can I do this?

jsfiddle

Please look at the below image. Drag and drop here box is there on top image cmng on down. I want to drop inside the box itself. Please help me to do this.

// Generated by CoffeeScript 1.6.3
// Couldn't get JSFiddle to work with CoffeeScript as advertised - Link to CoffeeScript Gist: https://gist.github.com/lsiv568/5623361
(function() {
  'use strict';
  angular.module('reusableThings', []).directive('fileDropzone', function() {
    return {
      restrict: 'A',
      scope: {
        file: '=',
        fileName: '='
      },
      link: function(scope, element, attrs) {
        var checkSize, isTypeValid, processDragOverOrEnter, validMimeTypes;
        processDragOverOrEnter = function(event) {
          if (event != null) {
            event.preventDefault();
          }
          event.dataTransfer.effectAllowed = 'copy';
          return false;
        };
        validMimeTypes = attrs.fileDropzone;
        checkSize = function(size) {
          var _ref;
          if (((_ref = attrs.maxFileSize) === (void 0) || _ref === '') || (size / 1024) / 1024 < attrs.maxFileSize) {
            return true;
          } else {
            alert("File must be smaller than " + attrs.maxFileSize + " MB");
            return false;
          }
        };
        isTypeValid = function(type) {
          if ((validMimeTypes === (void 0) || validMimeTypes === '') || validMimeTypes.indexOf(type) > -1) {
            return true;
          } else {
            alert("Invalid file type.  File must be one of following types " + validMimeTypes);
            return false;
          }
        };
        element.bind('dragover', processDragOverOrEnter);
        element.bind('dragenter', processDragOverOrEnter);
        return element.bind('drop', function(event) {
          var file, name, reader, size, type;
          if (event != null) {
            event.preventDefault();
          }
          reader = new FileReader();
          reader.onload = function(evt) {
            if (checkSize(size) && isTypeValid(type)) {
              return scope.$apply(function() {
                scope.file = evt.target.result;
                if (angular.isString(scope.fileName)) {
                  return scope.fileName = name;
                }
              });
            }
          };
          file = event.dataTransfer.files[0];
          name = file.name;
          type = file.type;
          size = file.size;
          reader.readAsDataURL(file);
          return false;
        });
      }
    };
  });

}).call(this);

(function() {
  'use strict';  
    angular.module('reusableThings').controller('TestCtrl', function($scope) {
        $scope.image = null
        $scope.imageFileName = ''
    });

}).call(this);
.dropzone {
  width: 250px;
  height: 45px;
  border: 1px solid #ccc;
  text-align: center;
  padding: 30px;
  margin: 20px;
  font-family: Arial;
}

.image-container {
    width: 300px;
    height: 300px;
}

.image-container img {
   max-width: 100%;   
}

.file-name {
   font-family: Arial;   
}
<div ng-app="reusableThings" ng-controller="TestCtrl">
    <div class="dropzone" file-dropzone="[image/png, image/jpeg, image/gif]"
                  file="image" file-name="imageFileName" data-max-file-size="3">
      <span>Drop Image Here</span>
    </div>
    <div class="image-container">
      <img ng-src={{image}} />
      <span class="file-name">{{imageFileName}}</span>
    </div>
</div>

Image

1 Answer 1

2

If I understood correctly, you want the drag'n'dropped image to render on top of the box, not after it. I think I worked it out. Find JSFiddle here

Changes:

  • Added CSS to .dropzone to fix it's position

.dropzone {
    width: 250px;
    height: 45px;
    border: 1px solid #ccc;
    text-align: center;
    padding: 30px;
    margin: 20px;
    font-family: Arial;

    /* New CSS */
    position: absolute;
    top: 0;
    left: 0;
}
  • Added CSS to .image-container to fix it's position on top of .dropzone's position. Also adjusted width to fully match that of .dropzone and added z-index: 2; so that the image appears on top.

.image-container {
    width: 312px;
    height: 312px;
    margin: 20px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
}
  • Added ng-show="image" on the .image-container so that it's hidden when there's no image, because otherwise the image drag'n'drop target would be this element instead of the .dropzone, due to the z-index.

<div class="image-container" ng-show="image">
  <img ng-src={{image}} />
  <span class="file-name">{{imageFileName}}</span>
</div>

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.