Created
November 24, 2014 23:29
-
-
Save UnbrandedTech/6b4b70d05a8198fb6b94 to your computer and use it in GitHub Desktop.
Bootstrap/Angular file upload or url input with preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$scope.removeImages = function(){ | |
var file = document.getElementById('imagePreview'); | |
while (file.firstChild) { | |
file.removeChild(file.firstChild); | |
} | |
delete $scope.icon.file; | |
}; | |
$scope.setFile = function(element) { | |
$scope.$apply(function($scope) { | |
$scope.icon.file = element.files[0]; | |
var imageType = /image.*/; | |
if ($scope.icon.file.type.match(imageType)) { | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
var img = new Image(); | |
img.src = reader.result; | |
img.className = 'img-responsive img-thumbnail'; | |
var file = document.getElementById('imagePreview'); | |
while (file.firstChild) { | |
file.removeChild(file.firstChild); | |
} | |
file.appendChild(img); | |
}; | |
reader.readAsDataURL($scope.icon.file); | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.btn-file { | |
position: relative; | |
overflow: hidden; | |
} | |
.btn-file input[type=file] { | |
position: absolute; | |
top: 0; | |
right: 0; | |
min-width: 100%; | |
min-height: 100%; | |
font-size: 100px; | |
text-align: right; | |
filter: alpha(opacity=0); | |
opacity: 0; | |
outline: none; | |
background: white; | |
cursor: inherit; | |
display: block; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<label for="name" class="col-sm-2 control-label">Icon</label> | |
<div class="col-sm-10"> | |
<div class="input-group"> | |
<input type="text" class="form-control" ng-disabled="icon.file" ng-model="icon.url"> | |
<span class="input-group-btn"> | |
<button ng-if="icon.file" ng-click="removeImages()" class="btn btn-danger btn-file">Cancel</button> | |
<span ng-if="!icon.file" class="btn btn-primary btn-file" ng-class="{'disabled' : icon.url }"> | |
Browse… <input onchange="angular.element(this).scope().setFile(this)" type="file" > | |
</span> | |
</span> | |
</div> | |
</div> | |
<div ng-if="icon.url" style="margin-top: 15px;" class="col-sm-10 col-sm-offset-2"> | |
<img class="img-thumbnail img-responsive" ng-src="{{icon.url}}"> | |
</div> | |
<div ng-if="icon.file" style="margin-top: 15px;" class="col-sm-10 col-sm-offset-2"> | |
<div id="imagePreview"></div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment