我想從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();
}
}
,服務的FileUpload:
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 is not an object (evaluating 'uploadResult.response')
callbackFromNativecordova.js:306
(función anónima)cordova.js:1102
nativeEvalAndFetchcordova.js:1110
nativeCallbackcordova.js:1099
(función anónima)index.html:1
使用的console.log我測試過,在服務的FileUpload發生線fd.append("data", JSON.stringify(data));
和線console.log("response ok: "+JSON.stringify(data));
這是什麼錯誤的錯誤? ??
你有調試過,並檢查你的帖子的迴應是什麼? – uksz
不要忘記iOS9運輸安全政策。你需要告訴它關於你正在與之通信的任何服務器的URL –
我已經檢查了帖子的回覆,是用'console.log(「response ok:」+ JSON.stringify(data))打印的;'它讓我好。必須如此。但在響應出現之前就出現了錯誤。這是奇怪的。 –