2017-02-23 62 views
1

得到文件後,我們得到了文件,它位於緩存中,我看到它在控制檯 但是當我試圖將其保存和發送,我得到了這個cordova.js失誤錯誤從相機

Entry.copyTo的參數「newName」錯誤類型:期望的字符串,但得到了Number。

未捕獲的TypeError:Entry.copyTo的參數「newName」的類型錯誤:期望的字符串,但獲得了Number。

成功錯誤callbackId:File1917405046:TypeError:Entry.copyTo參數「newName」的錯誤類型:Expected String,但獲得Number。安裝

ngCordova並注入 科爾多瓦更新 ,我不能把我的文件,請幫我
這是我在控制器代碼


$scope.attachPhoto = function() { 
     $ionicActionSheet.show({ 
      buttons: [ 
       { text: '<i class="icon ion-android-image"></i>Перейти в галерею' }, 
       { text: '<i class="icon ion-android-camera"></i> Сделать фото' } 
      ], 
      cancelText: 'Cancel', 
      cancel: function() { 

      }, 
      buttonClicked: function(index) { 
       var options = { 
        destinationType : Camera.DestinationType.FILE_URI, 
        sourceType : Camera.PictureSourceType.CAMERA, 
        allowEdit : false, 
        encodingType: Camera.EncodingType.JPEG, 
        popoverOptions: CameraPopoverOptions 
       }; 

       $cordovaCamera.getPicture(options).then(function(imageData) { 
        onImageSuccess(imageData); 
        console.log(imageData); 

        function onImageSuccess(fileURI) { 
         createFileEntry(fileURI); 
         console.log(fileURI); 
        } 
        function createFileEntry(fileURI) { 
         window.resolveLocalFileSystemURL(fileURI, copyFile, fail); 
         console.log(fileURI); 
        } 
        function copyFile(fileEntry) { 
         fileEntry.file(function(file) { 
          var reader = new FileReader(); 

          reader.onloadend = function(e) { 
           var imgBlob = new Blob([ this.result ], { type: "image/jpeg" }); 
           $scope.attach = true; 
           $scope.file = imgBlob; 
          }; 
          reader.readAsArrayBuffer(file); 
         }); 
         window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) { 
          fileEntry.copyTo(
           fileSystem2, 
           12345, 
           onCopySuccess, 
           fail 
          ); 
         }, fail); 
        } 
        function onCopySuccess(entry) { 
         $scope.$apply(function() { 
          $scope.images.push(entry.nativeURL); 
          $scope.attach = true; 
          $scope.sendPhoto(); 
         }); 
        } 
        function fail(error) { 
         console.log("fail: " + error.code); 
        } 
       }, function(err) { 
        console.log(err); 
       }); 
       console.log(options); 
       return true; 
      } 
     }) 
    }; 


    $scope.sendPhoto = function() { 

     var data = { 
      file: $scope.file 
     } 
     console.log(data); 
     var fd = new FormData(data); 

     xhr = new XMLHttpRequest(); 
     xhr.open("POST", "http://eatmeet.ru/serv.php"); 
     xhr.setRequestHeader('Content-Type', 'application/upload'); 
     xhr.send(fd); 

    } 
+0

請給一些反饋標誌。 – lin

回答

0

嘗試解析一個string作爲文件名,而不是number。請檢查file documentation

正確的語法是: fileEntry.copyTo(parent [DirectoryEntry], newName [DOMString], successCallback [Function], errorCallback [Function]);

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) { 
    fileEntry.copyTo(
     fileSystem2, 
     "12345", 
     onCopySuccess, 
     fail 
    ); 
}, fail); 
+0

非常感謝,這段代碼幫助了我,並且文件正在向服務器發送,但是我有了新的錯誤 – markprog

+0

@markprog很高興爲您提供幫助。你可能會將這個答案標記爲正確的答案? (投票按鈕附近的勾號) – lin