|
5 | 5 | <link type="text/css" rel="stylesheet" href="common.css">
|
6 | 6 | <title>Angular file upload sample</title>
|
7 | 7 | <script src="js/angular-file-upload-shim.js"></script>
|
8 |
| -<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script> |
9 |
| -<!-- <script src="http://cdn.jsdelivr.net/angular.file-upload/1.0.0/angular-file-upload.min.js"></script> --> |
| 8 | +<script type="text/javascript"> |
| 9 | +// load angularjs specific version |
| 10 | +var angularVersion = window.location.hash.substring(1); |
| 11 | +document.write('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/' + (angularVersion || '1.2.0') + '/angular.js"><\/script>'); |
| 12 | +</script> |
10 | 13 | <script src="js/angular-file-upload.js"></script>
|
| 14 | +<!-- <script src="http://cdn.jsdelivr.net/angular.file-upload/1.0.0/angular-file-upload.min.js"></script> --> |
11 | 15 | </head>
|
12 | 16 |
|
13 |
| -<body> |
| 17 | +<body ng-app="fileUpload" ng-controller="MyCtrl"> |
| 18 | + <div class="ng-v"> |
| 19 | + Angular Version: <input type="text" ng-model="angularVersion"> |
| 20 | + <input type="button" data-ng-click="changeAngularVersion()" value="Go"> |
| 21 | + </div> |
14 | 22 | <h1>Angular file upload Demo</h1>
|
15 | 23 | <h3>
|
16 | 24 | Visit <a href="https://github.com/danialfarid/angular-file-upload">angular-file-upload</a> on github
|
17 | 25 | </h3>
|
18 | 26 |
|
19 |
| - <div ng-app="fileUpload" ng-controller="MyCtrl"> |
| 27 | + <div> |
20 | 28 | Model object to be sent to the server with the file: <input type="text" ng-model="myModel"> <br />
|
21 | 29 | choose a single file: <input type="file" ng-file-select="onFileSelect($files)">
|
22 | 30 | <br/>
|
|
26 | 34 | <div ng-show="dropSupported" class="drop-box" ng-file-drop="onFileSelect($files);" ng-file-drop-available="dropSupported=true">or drop files here</div>
|
27 | 35 | <div ng-show="!dropSupported">HTML5 Drop File is not supported on this browser</div>
|
28 | 36 | <br/>
|
| 37 | + <input type="checkbox" ng-model="uploadRightAway">Upload right away |
| 38 | + <br/> |
| 39 | + <br/> |
29 | 40 | Progress:
|
30 | 41 | <br/>
|
| 42 | + <br/> |
31 | 43 | <div ng-show="selectedFiles != null">
|
32 | 44 | <div class="sel-file" ng-repeat="f in selectedFiles">
|
33 |
| - <span class="progress"> |
| 45 | + <img ng-show="dataUrls[$index]" src="{{dataUrls[$index]}}"> |
| 46 | + <button class="button" ng-click="start($index)" ng-show="progress[$index] < 0">Start</button> |
| 47 | + <span class="progress" ng-show="progress[$index] >= 0"> |
34 | 48 | <div style="width:{{progress[$index]}}%">{{progress[$index]}}%</div>
|
35 | 49 | </span>
|
36 |
| - <button class="button" ng-click="upload[$index].abort(); upload[$index]=null" |
37 |
| - ng-show="upload[$index] && progress[$index] < 100">Abort</button> |
| 50 | + <button class="button" ng-click="abort($index)" ng-show="hasUploader($index) && progress[$index] < 100">Abort</button> |
38 | 51 | {{f.name}} - size: {{f.size}}B - type: {{f.type}}
|
39 | 52 | </div>
|
40 | 53 | </div>
|
|
58 | 71 | <script type="text/javascript">
|
59 | 72 | angular.module('fileUpload', [ 'angularFileUpload' ]);
|
60 | 73 |
|
61 |
| - var MyCtrl = [ '$scope', '$http', '$timeout', '$upload', function($scope, $http, $timeout, $upload) { |
62 |
| - |
| 74 | + var MyCtrl = [ '$scope', '$http', '$timeout', '$upload', function($scope, $http, $timeout, $upload) { |
| 75 | + $scope.uploadRightAway = true; |
| 76 | + $scope.changeAngularVersion = function() { |
| 77 | + window.location.hash = $scope.angularVersion; |
| 78 | + window.location.reload(true); |
| 79 | + } |
| 80 | + $scope.hasUploader = function(index) { |
| 81 | + return $scope.upload[index] != null; |
| 82 | + }; |
| 83 | + $scope.abort = function(index) { |
| 84 | + $scope.upload[index].abort(); |
| 85 | + $scope.upload[index] = null; |
| 86 | + }; |
| 87 | + $scope.angularVersion = window.location.hash.length > 1 ? window.location.hash.substring(1) : '1.2.0'; |
63 | 88 | $scope.onFileSelect = function($files) {
|
64 | 89 | $scope.selectedFiles = [];
|
65 | 90 | $scope.progress = [];
|
|
73 | 98 | $scope.upload = [];
|
74 | 99 | $scope.uploadResult = [];
|
75 | 100 | $scope.selectedFiles = $files;
|
| 101 | + $scope.dataUrls = []; |
76 | 102 | for ( var i = 0; i < $files.length; i++) {
|
77 | 103 | var $file = $files[i];
|
78 |
| - $scope.progress[i] = 0; |
79 |
| - (function(index) { |
80 |
| - $scope.upload[index] = $upload.upload({ |
81 |
| - url : 'upload', |
82 |
| - headers: {'myHeaderKey': 'myHeaderVal'}, |
83 |
| - data : { |
84 |
| - myModel : $scope.myModel |
85 |
| - }, |
86 |
| - /* formDataAppender: function(fd, key, val) { |
87 |
| - if (angular.isArray(val)) { |
88 |
| - angular.forEach(val, function(v) { |
89 |
| - fd.append(key, v); |
90 |
| - }); |
91 |
| - } else { |
92 |
| - fd.append(key, val); |
93 |
| - } |
94 |
| - }, */ |
95 |
| - file : $file, |
96 |
| - fileFormDataName: 'myFile' |
97 |
| - }).then(function(response) { |
98 |
| - $scope.uploadResult.push(response.data.result); |
99 |
| - }, null, function(evt) { |
100 |
| - $scope.progress[index] = parseInt(100.0 * evt.loaded / evt.total); |
101 |
| - }); |
102 |
| - })(i); |
| 104 | + if (window.FileReader && $file.type.indexOf('image') > -1) { |
| 105 | + var fileReader = new FileReader(); |
| 106 | + fileReader.readAsDataURL($files[i]); |
| 107 | + function setPreview(fileReader, index) { |
| 108 | + fileReader.onload = function(e) { |
| 109 | + $timeout(function() { |
| 110 | + $scope.dataUrls[index] = e.target.result; |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + setPreview(fileReader, i); |
| 115 | + } |
| 116 | + $scope.progress[i] = -1; |
| 117 | + if ($scope.uploadRightAway) { |
| 118 | + $scope.start(i); |
| 119 | + } |
103 | 120 | }
|
104 | 121 | }
|
| 122 | + |
| 123 | + $scope.start = function(index) { |
| 124 | + $scope.progress[index] = 0; |
| 125 | + $scope.upload[index] = $upload.upload({ |
| 126 | + url : 'upload', |
| 127 | + headers: {'myHeaderKey': 'myHeaderVal'}, |
| 128 | + data : { |
| 129 | + myModel : $scope.myModel |
| 130 | + }, |
| 131 | + /* formDataAppender: function(fd, key, val) { |
| 132 | + if (angular.isArray(val)) { |
| 133 | + angular.forEach(val, function(v) { |
| 134 | + fd.append(key, v); |
| 135 | + }); |
| 136 | + } else { |
| 137 | + fd.append(key, val); |
| 138 | + } |
| 139 | + }, */ |
| 140 | + file: [$scope.selectedFiles[index], $scope.selectedFiles[index + 1]], |
| 141 | + //fileFormDataName: 'myFile' |
| 142 | + }).then(function(response) { |
| 143 | + $scope.uploadResult.push(response.data.result); |
| 144 | + }, null, function(evt) { |
| 145 | + $scope.progress[index] = parseInt(100.0 * evt.loaded / evt.total); |
| 146 | + }); |
| 147 | + } |
105 | 148 | } ];
|
106 | 149 | </script>
|
107 | 150 | </body>
|
|
0 commit comments