2016-01-27 25 views
0

我想從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));

這是什麼錯誤的錯誤? ??

+2

你有調試過,並檢查你的帖子的迴應是什麼? – uksz

+1

不要忘記iOS9運輸安全政策。你需要告訴它關於你正在與之通信的任何服務器的URL –

+0

我已經檢查了帖子的回覆,是用'console.log(「response ok:」+ JSON.stringify(data))打印的;'它讓我好。必須如此。但在響應出現之前就出現了錯誤。這是奇怪的。 –

回答

1

您確定錯誤發生在HTTP POST之前嗎?

好像在文件上傳服務失敗的情況下,返回一個字符串,但是當您嘗試訪問的結果,你總是當作一個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上傳服務。

祝你好運!

+0

我打算檢查一下。也許你擋道了。給5分鐘 –

+0

你是完全正確的。驗證解決了一些問題,服務器返回的數據不是我想的對象,錯誤在inf條件中。 –

+1

我會看到一些ng-file-upload,非常感謝大家。 –