javascript - 上传带有进度百分比的 angularjs 文件

标签 javascript angularjs file-upload multipartform-data

在我的网络应用程序中,我有一项使用 angularjs 和分段上传上传文件的服务。这是一个示例:https://jsfiddle.net/ZG9re/3909/ 它工作完美,但我不明白如何在上传过程中看到文件的百分比。我不想使用 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:

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

感谢帮助

最佳答案

您的代码所做的就是进行 $http POST 来上传文件。由于这是标准 HTTP 事务,因此在出现 TIMEOUT、SUCCESS (2xx) 或 FAILURE 之前,您不会从服务器获得响应。

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

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

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

它允许您确定进度。

与进度条结合使用

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

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

我之前曾在专业 SPA 中让这两个人一起工作过。

希望这有帮助。

ng-file-upload 的方法是......

$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 个 THEN 方法函数是 SUCCESS、FAILURE、EVENT(进度)

我怀疑 $http THEN 方法是否支持第三个 EVENT 函数,但你可以尝试一下。

关于javascript - 上传带有进度百分比的 angularjs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042623/

相关文章:

javascript - 表中每个控件的唯一 ID

javascript - Angular + Bootstrap 中的事件按钮状态

JavaScript:BannerPlugin.js 导致错误:str.indexOf 不是函数

php - 使用 v4 创建上传表单文件到 s3

php - CodeIgniter 3 - 上传文件错误: Undefined property/Call to a member function on null

javascript - 当使用node js存在同名文件时如何处理文件上传

php - 通过单击 PHP 中的单选按钮设置 session ?

javascript - ng-attr-data-toggle 简写 : display nothing for false

javascript - 我们可以将 ng-bind 值存储到 php 字符串吗

javascript - 如何通过 Angular Controller 将参数传递给服务方法?