【发布时间】:2016-05-08 18:46:56
【问题描述】:
概述: 我正在使用离子框架和 AngularJS 创建一个应用程序。该应用程序有 2 种形式,用户可以上传图像。在第一种形式中,图片上传字段只允许上传一张图片,第二种形式的图片上传字段允许上传多张图片。用户上传图像后,我在图像字段下方向用户显示预览。然后用户点击“保存”,调用Web Service(定义为工厂)将图像上传到站点。 Service函数实现了$q,让表单上传后才能继续提交。这工作正常。
问题: 上传多个图像时,我在控制器中调用另一个辅助函数,该函数循环(forEach)文件数据并为每个文件调用文件保存函数。但是这个辅助函数中的执行不会等待 forEach 循环完成。这使我可以获取已保存文件的第一个文件详细信息,但不能获取剩余已保存文件的详细信息。
这是我用于上传文件的代码:
.controller('AppCtrl', function($q) {
// Upload single file.
$scope.fileUpload = function(fileData) {
var deferred = $q.defer();
if (angular.isUndefined(fileData) || angular.isUndefined(fileData.dataURL)) {
deferred.resolve("No new upload found.");
}
else {
var filedata = {
"file" : {
file: (fileData.dataURL).replace(/^data:image\/[A-Za-z]{3,4};base64,+/g, ''),
filename: fileData.file.name,
},
};
AuthServiceContent.uploadNewFile(filedata).then(function(response) {
deferred.resolve(response);
}, function(response) {
deferred.reject(response);
});
}
return deferred.promise;
};
// Upload multiple files.
$scope.fileUploadMultiple = function(data) {
var deferred = $q.defer();
var fileUploadStatus = [];
if (angular.isUndefined(data) || angular.isUndefined(data[0])) {
deferred.reject("No new upload found.");
}
else {
angular.forEach(data, function(fileData, index) {
$scope.fileUpload(fileData).then(function(response) {
fileUploadStatus[index] = response;
}, function(response) {
});
});
}
return (!angular.isUndefined(fileUploadStatus) ? deferred.resolve(fileUploadStatus) : deferred.reject(fileUploadStatus));
};
$scope.createContent = function(formData) {
$scope.fileUploadMultiple(formData.image).then(function(response) {
if (angular.isObject(response)) {
angular.forEach(response, function(fileData, index) {
console.log(fileData);
formData.image.und = [{'fid' : fileData.fid}];
});
console.log(formData);
}
else {
}
}, function(err) {
console.log("updates failed!!!");
});
return;
};
})
.factory('AuthServiceContent', function($q, $http, DEFAULT) {
var service_content = {
uploadNewFile: function(fileData) {
var deferred = $q.defer();
$http.post(DEFAULT.serviceURL + 'file', JSON.stringify(fileData), {
headers : {
'Content-Type': 'application/json',
'Cookie': 'cookieData',
}
})
.success(function (data, status, headers, config) {
deferred.resolve(data);
})
.error(function (data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;
}
};
return service_content;
});
我已经尝试了超过 2 天,发现了类似的问题,但它不起作用。
更新: 我通过添加额外的检查循环来完成这项工作,这里是控制器中的更新功能以上传多个图像。
$scope.fileUploadMultiple = function(data) {
var deferred = $q.defer();
var fileUploadStatus = [];
if (angular.isUndefined(data) || angular.isUndefined(data[0])) {
deferred.reject("No new upload found.");
}
else {
var dataLength = (data.length - 1);
angular.forEach(data, function(fileData, index) {
$scope.fileUpload(fileData).then(function(response) {
fileUploadStatus[index] = response;
// Check if we are at last element. If yes, then return status
// Return deffered status on last element.
if (dataLength == index) {
deferred.resolve(fileUploadStatus);
}
}, function(response) {
// Check if we are at last element. If yes, then return status
// Return deffered status on last element.
if (dataLength == index) {
deferred.reject(fileUploadStatus);
}
});
});
}
return deferred.promise;
};
【问题讨论】:
-
我试过你更新的代码。如果您上传多个文件并且最后一个是最小的,它会触发 resolve。 fileUploadStatus 中没有较大的文件。
标签: angularjs file-upload ionic-framework execution