离子3图像通过相机和相机画廊上传

时间:2018-03-30 17:01:53

标签: angular ionic3

我附上了我的ts文件代码。我无法将图像发送到服务器。似乎正常工作意味着我收到通知您的图像已成功上传。但我无法看到变化。请帮忙 - 谢谢

public presentActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
        title: 'Select Image Source',
        buttons: [
                     { text: 'Load from Library',
                       handler: () => {
                             this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
                       }
                     },
                     { text: 'Use Camera',
                       handler: () => {
                            this.takePicture(this.camera.PictureSourceType.CAMERA);
                       }
                     },
                     { text: 'Cancel', role: 'cancel' }
                 ]
    });
    actionSheet.present();
}

1 个答案:

答案 0 :(得分:0)

您提供的代码仅用于显示在库和相机本身之间选择源之间选择的操作表。没有代码可以将其上传到服务器。

我希望上传到firebase存储(我个人最喜欢的)

uploadPic() {
let that=this
let imagefile = this.base64Image.split(',')[1] //this gets rid of the "data:image/jpeg;base64," part of the file

let uploadTask = firebase
  .storage()
  .ref(this.uid)
  .child("profile_pic")
  .putString(imagefile, "base64", { contentType: "image/jpg" });
uploadTask.on(
  firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  function(snapshot:any) {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = snapshot.bytesTransferred / snapshot.totalBytes * 100;
    console.log("Upload is " + progress + "% done");
    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log("Upload is paused");
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log("Upload is running");
        break;
    }
  },
  function(error) {
    alert("error:"+JSON.stringify(error));
  },
  function() {
    // Upload completed successfully, now we can get the download URL
    console.log("got to downloadURL")
    that.downloadURL = uploadTask.snapshot.downloadURL;
    that.updateAuthUser();
  }
);

}

您可以在此处的firebase文档中找到类似的代码:https://firebase.google.com/docs/storage/web/upload-files

如果您为上传的图片使用google firebase存储的其他目标服务器,则上述代码会有所不同。