Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 上载带有进度百分比的angularjs文件_Javascript_Angularjs_File Upload_Multipartform Data - Fatal编程技术网

Javascript 上载带有进度百分比的angularjs文件

Javascript 上载带有进度百分比的angularjs文件,javascript,angularjs,file-upload,multipartform-data,Javascript,Angularjs,File Upload,Multipartform Data,在我的webapp中,我有一个使用angularjs和multipartupload上传文件的服务。这是一个例子: 它工作得很好,但我不明白在上传过程中怎么能看到文件的百分比。我不想使用XMLHttpRequest,但如果可能的话,我需要保留这段代码。这就是代码: var myApp = angular.module('myApp', []); myApp.directive('fileModel', ['$parse', function ($parse) { return {

在我的webapp中,我有一个使用angularjs和multipartupload上传文件的服务。这是一个例子: 它工作得很好,但我不明白在上传过程中怎么能看到文件的百分比。我不想使用XMLHttpRequest,但如果可能的话,我需要保留这段代码。这就是代码:

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:


感谢您的帮助

您的代码所做的只是制作一个$http POST来上传文件。因为这是一个标准的HTTP事务,所以在超时或成功2xx或失败之前,您不会从服务器获得响应

因此,使用当前代码,您无法执行此操作

但是,有一个名为ng file upload的模块

这允许您确定进度

将其与进度条结合使用

见-

您可以向用户提供良好的反馈:

我之前在一家专业水疗中心让这两个人一起工作过

希望这有帮助

ng文件上传的方法是

$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);
    });
    };
然后,3个方法函数是成功、失败、事件进度

我怀疑$http THEN方法是否支持第三个事件函数,但您可以试一试

$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);
    });
    };