【发布时间】:2016-12-11 19:29:30
【问题描述】:
我们在加载从 S3 获取的文档 url 并在仪表板标签页上显示缩略图时面临挑战。
/***fetch docs **push success confirm***/
$scope.getdocuments = function(){
DDListDataService.getDocumentTypes().then(function(docs){
$scope.docTypes = docs.data;
});
userService.fetchUserDocs($scope.user.customerId,$scope.user.entityType).then(function(userDocs){
//console.log("result--"+userDocs);
angular.forEach(userDocs, function(object , key){
console.log(object);
var xhr = new XMLHttpRequest();
var blob;
xhr.open('GET', object.documents[0].documentUrl);
//var file = common.base64ToBlob(object.d, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
//var file = remoteToBlob(object.documents[0].documentUrl);
//var file = common.base64ToBlob(object.documents[0].document, 'image/jpg');
/*file.lastModifiedDate = new Date();
file.name = "";*/
var file = new Blob([this.response], {type: 'image/jpg'});
//rest of the code that uses the blob goes here
file.lastModifiedDate = new Date();
file.name = "";
//setTimeout(function(){console.log(file)},1000);
var options = {
customerId : $scope.user.customerId,
customerType : $scope.user.entityType,
docTypeName : object.key.documentName,
documentName : object.key.documentNameId,
documentVersion : object.key.documentVersion,
documentUID : object.doccumentUID,
//requestedBy : $scope.user.customerType,
requestedBy : LocalService.get('userType'),
requestedById : $scope.user.customerId
}
var fileItem = new FileUploader.FileItem($scope.uploader,file,options);
fileItem.progress = 100;
fileItem.isUploaded = true;
fileItem.isSuccess = true;
/*file.customerId = $scope.user.customerId;
file.customerType =$scope.user.entityType;
file.documentName = object.key.documentNameId;
file.documentVersion =object.key.documentVersion;
file.documentUID = object.doccumentUID;
file.requestedBy = $scope.user.customerId;
file.requestedById = $scope.user.customerId;*/
$scope.uploader.queue.push(fileItem);
console.log(fileItem);
};
xhr.send();
});
});
//$scope.uploader.formData=[{"user":angular.json($scope.user)}];
$scope.uploader.queue.length = 0;
}
xhr.send() 执行后,页面加载发生,但数据还没有准备好,它给出了未定义的文件对象,因为同时没有返回数据。在返回数据之前,我们如何才能拖拉负载?我认为 onload 有一个回调应该执行该功能。如果错误,请纠正我。
怎么处理?
显示图像的 html 是
<div ng-show="uploader.isHTML5" ng-thumb="{ file: item._file, height: 100 ,stream:item._file}"></div>
【问题讨论】:
-
你为什么不使用 angular ajax 语法?
-
Angular 的新手。您能解释一下它在 Angular 中的工作原理吗?它会解决我们面临的问题吗? @madalinivascu
-
你应该使用angularsee an example here提供的$http服务。
-
@oliv37 您能否建议对当前代码进行更正?我想让当前的工作正常运行,然后为此编写服务。
标签: javascript jquery angularjs ajax xmlhttprequest