0

我正在使用cordovaCamera插件在我的離子應用程序中獲取文件的URL。如何把文件放在離子中的火力點存儲

我想把這個文件放在firebase storage 不是base64

這裏是我的參考

$cordovaCamera.getPicture(options) 
.then(function(imageData) { 
var pthref = firebase.storage().ref().child("a/b"); 
pthref.put(WHAAT SHOULD GO HERE).then(function(snapshot) { 
alert('file uploaded!'); 
}, 
function(a) 
{ 
alert("error"+JSON.stringify(a))}); 
}, 
function(err) { 
alert("Camera Error") 
}); 
+0

「imageData」看起來是最合乎邏輯的一個。它不工作? –

+0

imageData返回文件的路徑。我需要把Blob或文件對象 – sam

回答

0

爲了使其運作的,你需要使用destinationType: Camera.DestinationType.FILE_URI來獲得圖像的URL代碼。

然後,您可以使用cordova-plugin-file(不要忘記添加它)並將其發送到Firebase,從此圖像中獲取Blob。

此代碼可能有語法錯誤,我將它從Ionic 2代碼轉換而來。

var options = { 
    destinationType: Camera.DestinationType.FILE_URI 
}; 
$cordovaCamera.getPicture(options) 
    .then(function (fileUrl) { 
     window.resolveLocalFileSystemURL(fileUrl, (fileEntry) => { 
      fileEntry.file((file) => { 
       let reader = new FileReader(); 
       reader.onloadend = function() { 
       let imgBlob = new Blob([ this.result ], { type: "image/jpeg" }); 
       // Send file to firebase here 
       var pthref = firebase.storage().ref().child("a/b"); 
       pthref.put(imgBlob); 
       }; 
       reader.onerror = (error) => { 
       console.error(error); 
       }; 
       reader.readAsArrayBuffer(file); 
      }, (error) => { 
       console.error(error); 
      }); 
     }) 
    }, 
    function (err) { 
     alert("Camera Error") 
    }); 
+0

我正在開發ionic1。更多'cordova-plugin-file'無法解析IOS中的路徑。我不能使用這個插件。我使用destinationType FILE_URI – sam