Upload angularjs file with progress percentage

人盡茶涼 提交于 2020-02-04 02:59:32

问题


In my webapp i've got a service to upload files using angularjs and multipart upload. This is an example: https://jsfiddle.net/ZG9re/3909/ It works perfectly but i can't understand how could see the percentage of the file during the upload. I don't want use XMLHttpRequest but i need to mantain this code if possible. This is the code anyway:

var myApp = angular.module('myApp', []);

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){

    $scope.uploadFile = function(){
        var file = $scope.myFile;
        console.log('file is ' );
        console.dir(file);
        var uploadUrl = "/fileUpload";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };

}]);

html:

<div ng-controller = "myCtrl">
    <input type="file" file-model="myFile"/>
    <button ng-click="uploadFile()">upload me</button>
</div>

thanks for help


回答1:


All your code is doing is making an $http POST to upload a file. Since this is a standard HTTP transaction, you won't get a response from the server until TIMEOUT or SUCCESS (2xx) or FAILURE.

Thus with your current code you cannot do this.

However, there is a module called ng-file-upload

https://github.com/danialfarid/ng-file-upload

which allows you to determine progress.

Using this in conjunction with a progress bar

see - http://angular-ui.github.io/bootstrap/#/progressbar

you can give good feedback to the user :)

I have had these 2 working together previously in a professional SPA.

Hope this helps.

The approach with ng-file-upload is ...

$scope.upload = function (file) {
    Upload.upload({
        url: 'upload/url',
        data: {file: file, 'username': $scope.username}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
    };

The 3 THEN method functions are SUCCESS, FAILURE, EVENT (progress)

I doubt the $http THEN method supports the 3rd EVENT function but you could give it a go.



来源:https://stackoverflow.com/questions/35042623/upload-angularjs-file-with-progress-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!