上传进度百分比的angularjs文件 [英] Upload angularjs file with progress percentage
问题描述
在我的Web应用程序中,我有一项服务,可以使用angularjs和分段上传来上传文件.这是一个示例: https://jsfiddle.net/ZG9re/3909/
它工作正常,但我不明白在上传过程中怎么能看到文件的百分比.我不想使用XMLHttpRequest
,但如果可能的话,我需要保留此代码.反正这是代码:
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>
感谢帮助
推荐答案
您的所有代码正在做的是执行$ http POST来上传文件.由于这是标准的HTTP事务,因此直到TIMEOUT或SUCCESS(2xx)或FAILURE都不会收到服务器的响应.
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.
但是,有一个名为ng-file-upload的模块
However, there is a module called ng-file-upload
https://github.com/danialfarid/ng-file-upload
它可以让您确定进度.
将此与进度条结合使用
请参阅- http://angular-ui.github.io/bootstrap/#/进度栏
您可以向用户提供良好的反馈意见:)
you can give good feedback to the user :)
我以前曾在专业SPA中将这2个功能一起使用.
I have had these 2 working together previously in a professional SPA.
希望这会有所帮助.
使用ng-file-upload的方法是...
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);
});
};
3种THEN方法的功能是成功,失败,事件(进行)
The 3 THEN method functions are SUCCESS, FAILURE, EVENT (progress)
我怀疑$ http THEN方法是否支持第3个EVENT函数,但您可以尝试一下.
I doubt the $http THEN method supports the 3rd EVENT function but you could give it a go.
这篇关于上传进度百分比的angularjs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!