2015-06-22 40 views
0

我想從我的應用程序上傳圖像到服務器,我想使用ngCordova文件傳輸插件,但我不滿意這個插件給你的進度信息,所以我決定使用Flowjs,但我無法設法從我擁有的文件url中創建一個合適的HTML5 File對象。 這是我的代碼Flowjs文件上傳 - Ionic和ngCordova

window.resolveLocalFileSystemURL(photo, function(fileEntry){ 
    fileEntry.file(function(file){ 
     $scope.flow.addFile(file); 
     $scope.flow.upload(); 
     $scope.flow.on('fileProgress', function (file, chunk){ 
      console.log(file); 
      console.log(chunk); 
     }); 
     $scope.flow.on('error', function(a,b){ 
      console.log(a); 
      console.log(b); 
     }); 
    }); 
    }, function(err){ 
     console.log(err); 
}); 

如果照片是路徑文件系統的文件。 當我嘗試做這種上傳時,我得到一個400(錯誤的請求)錯誤,我確定服務器端是正確的,因爲我用它與許多其他Flowjs應用程序。 我認爲fileEntry.file()返回的對象不是一個合適的HTML5文件對象,也許從文件url創建一個Blob可以解決問題,但我不知道如何創建它。 我想讓我的代碼工作,雖然之前嘗試創建一個Blob,但如果這是唯一的解決方案,以及...

回答

0

好吧,我找到了一個解決方案的作品,不知道,如果它是最好的可能性,任何改進都非常感謝。

window.resolveLocalFileSystemURL(photo, function(fileEntry){ 
    fileEntry.file(function(file){ 
     var reader = new FileReader(); 
     reader.onload = function(){ 
      var blob = new Blob([reader.result], {type: 'application/octet-stream'}); 
      blob.name = 'image.jpg'; 
      $scope.flow.addFile(blob); 
      $scope.flow.upload(); 
      $scope.flow.on('fileProgress', function (file, chunk){ 
       console.log(file); 
       console.log(chunk); 
      }); 
      $scope.flow.on('error', function(a,b){ 
       console.log(a); 
       console.log(b); 
      }); 
     }; 
     reader.readAsArrayBuffer(file); 
    }); 
}, function(err){ 
    console.log(err); 
});