我正在尝试从iOS上传文件,但是我遇到了意外错误。这是我的HTML:
<div ng-repeat="photo in vendordata.galerias" class="elemento">
<div style="z-index: 10;">
<img class="thumb" ng-src="{{photo.url}}" />
</div>
<div class="" style="text-align: center;">
<button class="" href="#" ng-show="photo.borrar">Eliminar</button>
<button class="" href="#" ng-click="subirFoto($index+1)" ng-hide="photo.borrar">Subir foto</button>
</div>
</div>
控制器方法:
$scope.subirFoto = function(identificador){
$scope.elementoSek = identificador;
console.log("IMG src seleccionado:"+$scope.elementoSek);
$scope.getPhoto(pictureSource.PHOTOLIBRARY);
};
$scope.getPhoto = function (source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
};
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// Get image handle
var previo = $scope.vendordata.galerias[$scope.elementoSek].url;
var file = imageURI;
var uploadUrl = CLOSER_SERVER.url+'/upload_vendor';
var uploadResult = fileUpload.uploadFileToUrl(file, uploadUrl, $scope.elementoSek, Session.getId(), 'vendor');
console.log(uploadResult);
if(uploadResult.response == "200"){
$scope.vendordata.galerias[$scope.elementoSek].url = imageURI;
$scope.$apply();
}
}
服务文件上传:
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl, numero, sessionIde, typePic){
var fd = new FormData();
fd.append('file', file);
var data = {
session : sessionIde,
num_foto: numero,
type : typePic
};
fd.append("data", JSON.stringify(data));
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.then(function(data){
console.log("response ok: "+JSON.stringify(data));
return JSON.stringify(data);
}, function(){
console.log("response fail");
return "error subiendo archivo";
});
}
}]);
这是做什么的?在包含6个元素的图库中,单击“上传”(Subir)按钮,然后加载iOS图库以拾取图像。当我选择图像时,图像URI被发送到fileUpload服务。使用方法uploadFIleToUrl(),我将其发送到$http.post
的服务器。在这一刻它抛出了一个我无法理解的奇怪错误:
TypeError:undefined不是对象(评估'uploadResult.response')
callbackFromNativecordova.js:306
(funciónanónima)cordova.js:1102
nativeEvalAndFetchcordova.js:1110
nativeCallbackcordova.js:1099
(funciónanónima)index.html:1
使用console.log我检查过行fd.append("data", JSON.stringify(data));
和行console.log("response ok: "+JSON.stringify(data));
这是什么错误???
答案 0 :(得分:1)
您确定错误发生在HTTP帖子之前吗?
似乎在fileUpload服务失败的情况下,您返回一个字符串,但是当您尝试访问结果时,您始终将其视为JSON对象:
.then(function(data){
console.log("response ok: "+JSON.stringify(data));
return JSON.stringify(data);
}, function(){
console.log("response fail");
return "error subiendo archivo";
});
您需要在此处进行一些验证:
if(angular.isObject(uploadResult) && uploadResult.response == "200"){
$scope.vendordata.galerias[$scope.elementoSek].url = imageURI;
$scope.$apply();
}
你可以使用ng-file-upload,即使你不能/不想使用这些指令,你仍然可以使用de upload service。
祝你好运!