帮助我解决问题的最新代码
$scope.uploadedFiles = [];
$scope.upload = function(files) {
$scope.uploadedFiles = files;
angular.forEach(files, function(file) {
if (file && !file.$error) {
if($scope.uploadedFiles.indexOf(file) == -1) {
$scope.uploadedFiles.push(file);
}
file.upload = Upload.upload({
url: 'uploader',
file: file,
userId: $scope.user._id
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data.id;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
});
file.upload.progress(function (evt) {
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
});
console.log($scope.uploadedFiles);
}
$scope.removeUploadedFile = function(files, idx) {
files.splice(idx, 1);
console.log($scope.uploadedFiles);
$scope.uploadedFiles = files;
console.log($scope.uploadedFiles);
}
.form-group
.btn.btn-default.btn-file
i.fa.fa-paperclip
| Attach Files
input(type='file', ngf-select='upload($files)',accept="text/plain, image/*", ng-model="files", ngf-multiple="true", ngf-max-size='10000000', ngf-keep="true", ngf-keep-distinct="true")
p.help-block Max. 1MB
br
ul#files(ng-repeat="f in files", style='list-style-type: none; padding:0; margin:0;')
li
div
{{f.name}}
.pull-right
{{f.size | bytes}} #[i.fa.fa-close(ng-click="removeUploadedFile(files, $index)")]
div.progress(ng-show="f.progress >= 0")
div(style="width:{{f.progress}}%", ng-bind="f.progress + '%'")
我正在使用ng-file-upload来处理文件上传。除了我无法访问控制器中的文件外,一切正常。
这是我的控制器代码:
$scope.upload = function(files) {
uploadedFiles = files;
console.log($scope.files);
angular.forEach(files, function(file) {
if (file && !file.$error) {
file.upload = Upload.upload({
url: 'uploader',
file: file,
userId: $scope.user._id
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data.id;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
});
file.upload.progress(function (evt) {
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
});
console.log(uploadedFiles);
}
$scope.removeUploadedFile = function(files, idx) {
console.log(files);
files.splice(idx, 1);
uploadedFiles = files;
}
我的HTML代码:
.form-group
.btn.btn-default.btn-file
i.fa.fa-paperclip
| Attach Files
input(type='file', name='attachment', ngf-select='upload($files)',accept="text/plain, image/*", ng-model="files", ngf-multiple="true", ngf-max-size='10000000', ngf-keep="true", ngf-keep-distinct="true")
p.help-block Max. 1MB
br
//- ul(ng-repeat='file in uploadedFiles', style='list-style-type: none; padding:0; margin:0;')
ul#files(ng-repeat='f in files', style='list-style-type: none; padding:0; margin:0;')
li
div
{{f.name}}
.pull-right
{{f.size | bytes}} #[i.fa.fa-close(ng-click="removeUploadedFile(files, $index)")]
div.progress(ng-show="f.progress >= 0")
div(style="width:{{f.progress}}%", ng-bind="f.progress + '%'")
如果我打印$scope.files
,它什么都不打印。
任何指示我做错的地方都会受到赞赏。
更新
为什么我需要访问$scope.files
?文件上传功能正常工作,但我需要将上传的文件ID传递给我的后端系统,以便在我的数据库中保存该ID。现在要做到这一点,我需要一些$scope
变量,其中包含更新的文件列表。由于我允许用户删除上传的文件,因此我需要使用最新的有效上传文件列表更新一些$scope
变量。如果还不清楚,请告诉我。
答案 0 :(得分:2)
在版本7.0.12中修复了ngf-keep的一些问题。 您的原始帖子现在可以使用,或者您可以按照以下方式操作:https://jsfiddle.net/danialfarid/e7x1xs4a/
<button ngf-select="uploadFiles($files)" ngf-keep="true" multiple>Select Files</button>
$scope.uploadFiles = function(files) {
$scope.files = files;
console.log(files);
angular.forEach(files, function(file) {
if (file && !file.$error) {
file.upload = Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
file: file
});
file.upload.then(function (response) {"success"
}, function (response) {"error"});
file.upload.progress(function (evt) {"progress"});
}
});
}
答案 1 :(得分:0)
<强>编辑强>:
如果您想在每次添加新文件时更新文件,则需要在功能范围之外设置$scope.files
并在$scope.files
数组$scope.files.push(file);
中推送新文件:
$scope.files = [];
function uploadFiles(files) {
angular.forEach(files, function (file) {
$scope.files.push(file);
if (file && !file.$error) {
file.upload = Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
file: file
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
});
file.upload.progress(function (evt) {
file.progress = Math.min(100, parseInt(100.0 *
evt.loaded / evt.total));
});
}
});