Skip to content

Commit 393e4eb

Browse files
committed
Proper working simple implementation of ng-file-upload
1 parent de2cb9f commit 393e4eb

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

src/css/main.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ textarea.code {
238238
.upload-dragover {
239239
background-color: #EEEEEE;
240240
}
241+
.upload-dragover:before {
242+
content: "\e198";
243+
position: fixed;
244+
left: 50%;
245+
top: 50%;
246+
transform: translate(-50%, -50%);
247+
z-index: 100;
248+
color: rgba(102, 102, 102, 0.5);
249+
font-size: 80px;
250+
font-family: 'Glyphicons Halflings';
251+
font-style: normal;
252+
font-weight: normal;
253+
line-height: 1;
254+
-webkit-font-smoothing: antialiased;
255+
}
241256

242257
.upload-list {
243258
margin-top: 20px;

src/js/controllers/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,13 @@
316316

317317
$scope.removeFromUpload = function(index) {
318318
$scope.uploadFileList.splice(index, 1);
319-
};
319+
};
320320

321321
$scope.uploadFiles = function() {
322322
$scope.apiMiddleware.upload($scope.uploadFileList, $scope.fileNavigator.currentPath).then(function() {
323323
$scope.fileNavigator.refresh();
324+
$scope.uploadFileList = [];
325+
324326
$scope.modal('uploadfile', true);
325327
}, function(data) {
326328
var errorMsg = data.result && data.result.error || $translate.instant('error_uploading_files');

src/js/services/apihandler.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function(angular, $) {
22
'use strict';
3-
angular.module('FileManagerApp').service('apiHandler', ['$http', '$q', '$window', '$translate',
4-
function ($http, $q, $window, $translate) {
3+
angular.module('FileManagerApp').service('apiHandler', ['$http', '$q', '$window', '$translate', 'Upload',
4+
function ($http, $q, $window, $translate, Upload) {
55

66
$http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
77

@@ -114,23 +114,36 @@
114114
return deferred.promise;
115115
};
116116

117-
ApiHandler.prototype.upload = function(apiUrl, form) {
117+
ApiHandler.prototype.upload = function(apiUrl, destination, files) {
118118
var self = this;
119119
var deferred = $q.defer();
120120
self.inprocess = true;
121121
self.error = '';
122-
$http.post(apiUrl, form, {
123-
transformRequest: angular.identity,
124-
headers: {
125-
'Content-Type': undefined
126-
}
127-
}).success(function(data) {
128-
self.deferredHandler(data, deferred);
129-
}).error(function(data) {
130-
self.deferredHandler(data, deferred, 'Unknown error uploading files');
131-
})['finally'](function() {
132-
self.inprocess = false;
133-
});
122+
123+
var data = {
124+
destination: destination
125+
};
126+
127+
for (var i = 0; i < files.length; i++) {
128+
data['file-' + i] = files[i];
129+
}
130+
131+
if (files && files.length) {
132+
Upload.upload({
133+
url: apiUrl,
134+
data: data
135+
}).then(function (response) {
136+
self.deferredHandler(response.data, deferred);
137+
}, function (response) {
138+
if (response.status > 0) {
139+
self.deferredHandler(data, deferred, 'Unknown error uploading files (' + response.status + ')');
140+
}
141+
}, function (evt) {
142+
var progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
143+
144+
if(progress == 100) self.inprocess = false;
145+
});
146+
}
134147

135148
return deferred.promise;
136149
};

src/js/services/apimiddleware.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,14 @@
4040
return this.apiHandler.remove(fileManagerConfig.removeUrl, items);
4141
};
4242

43-
ApiMiddleware.prototype.upload = function(fileList, path) {
43+
ApiMiddleware.prototype.upload = function(files, path) {
4444
if (! $window.FormData) {
4545
throw new Error('Unsupported browser version');
4646
}
47-
var form = new $window.FormData();
48-
form.append('destination', this.getPath(path));
4947

50-
for (var i = 0; i < fileList.length; i++) {
51-
var fileObj = fileList.item(i);
52-
fileObj instanceof $window.File && form.append('file-' + i, fileObj);
53-
}
48+
var destination = this.getPath(path);
5449

55-
return this.apiHandler.upload(fileManagerConfig.uploadUrl, form);
50+
return this.apiHandler.upload(fileManagerConfig.uploadUrl, destination, files);
5651
};
5752

5853
ApiMiddleware.prototype.getContent = function(item) {

0 commit comments

Comments
 (0)