mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 05:57:57 +01:00
File upload progress
This commit is contained in:
parent
6aaecb473f
commit
d76b8e32c8
@ -1 +1,2 @@
|
||||
- Automatic backup system using Quartz (server)
|
||||
- Handle error while uploading a file
|
||||
|
@ -94,7 +94,7 @@ App.controller('DocumentEdit', function($rootScope, $scope, $q, $http, $state, $
|
||||
$scope.resetForm();
|
||||
$scope.loadDocuments();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (_.size($scope.newFiles) == 0) {
|
||||
navigateNext();
|
||||
@ -104,6 +104,13 @@ App.controller('DocumentEdit', function($rootScope, $scope, $q, $http, $state, $
|
||||
|
||||
// Send a file from the input file array and return a promise
|
||||
var sendFile = function(key) {
|
||||
var deferred = $q.defer();
|
||||
var getProgressListener = function(deferred) {
|
||||
return function(event) {
|
||||
deferred.notify(event);
|
||||
};
|
||||
};
|
||||
|
||||
// Build the payload
|
||||
var file = $scope.newFiles[key];
|
||||
var formData = new FormData();
|
||||
@ -111,20 +118,37 @@ App.controller('DocumentEdit', function($rootScope, $scope, $q, $http, $state, $
|
||||
formData.append('file', file);
|
||||
|
||||
// Send the file
|
||||
var promiseFile = $http.put('api/file',
|
||||
formData, {
|
||||
headers: { 'Content-Type': undefined },
|
||||
transformRequest: function(data) { return data; }
|
||||
$.ajax({
|
||||
type: 'PUT',
|
||||
url: 'api/file',
|
||||
data: formData,
|
||||
cache: false,
|
||||
contentType: false,
|
||||
processData: false,
|
||||
success: function(response) {
|
||||
deferred.resolve(response);
|
||||
},
|
||||
error: function(jqXHR, textStatus, errorThrown) {
|
||||
deferred.reject(errorThrown);
|
||||
},
|
||||
xhr: function() {
|
||||
var myXhr = $.ajaxSettings.xhr();
|
||||
myXhr.upload.addEventListener(
|
||||
'progress', getProgressListener(deferred), false);
|
||||
return myXhr;
|
||||
}
|
||||
});
|
||||
|
||||
// TODO Handle progression when $q.notify will be released
|
||||
|
||||
promiseFile.then(function() {
|
||||
$scope.fileProgress += 100 / _.size($scope.newFiles);
|
||||
// Update progress bar and title on progress
|
||||
var startProgress = $scope.fileProgress;
|
||||
deferred.promise.then(null, null, function(e) {
|
||||
var done = 1 - (e.total - e.position) / e.total;
|
||||
var chunk = 100 / _.size($scope.newFiles);
|
||||
$scope.fileProgress = startProgress + done * chunk;
|
||||
$rootScope.pageTitle = Math.round($scope.fileProgress) + '% - Sismics Docs';
|
||||
});
|
||||
|
||||
return promiseFile;
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
// Upload files sequentially
|
||||
|
Loading…
Reference in New Issue
Block a user