JSFiddle - React, Tailwind, and code Playground
by yahyaKACEM
HTML
<script src="http://angular-file-upload.appspot.com/js/ng-file-upload-shim.js"></script>
<script src="http://angular-file-upload.appspot.com/js/ng-file-upload.js"></script>
<div ng-app="fileUpload" ng-controller="MyCtrl">Username
<input type="text" ng-model="username"><br/><br/>
Drop File:
<div ngf-drop ngf-select ng-model="files" class="drop-box"
ngf-drag-over-class="dragover"
accept="image/*,application/pdf"
ngf-pattern="'image/*'">Drop pdfs or images here or click to upload</div>
<div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>
Files:
<ul>
<li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
</ul>
Upload Log:
<pre>{{log}}</pre>
</div>
</div>
CSS
.button {
-moz-appearance: button;
/* Firefox */
-webkit-appearance: button;
/* Safari and Chrome */
padding: 10px;
margin: 10px;
width: 70px;
}
.drop-box {
background: #F8F8F8;
border: 5px dashed #DDD;
width: 200px;
height: 65px;
text-align: center;
padding-top: 25px;
margin: 10px;
}
.dragover {
border: 5px dashed blue;
}
JavaScript
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.$watch('files', function () {
$scope.upload($scope.files);
});
$scope.$watch('file', function () {
if ($scope.file != null) {
$scope.files = [$scope.file];
}
});
$scope.log = '';
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.$error) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
username: $scope.username,
file: file
}
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
$scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.data.file.name + '\n' + $scope.log;
}).success(function (data, status, headers, config) {
$timeout(function() {
$scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
});
});
}
}
}
};
}]);