Ionic, upload image or string to firebase not work

2019-09-12 03:53发布

问题:

since about 3 days I am trying to upload an image to the firebase storage without success.

I tried several approaches I found here in stackoverflow. It is even not possible to upload a simple string for Android.

Running the app in the browser is working fine for images and strings.

Emulator and phone returns the same error: Firebase Storage: An unknown error occurred, please check the error payload for server response.

I don't know where should I check the mentioned "payload"

This is my code for uploading the string: EDIT: I changed the function which retrieves the error to

alert(error.serverResponse);

this returns following Error message: "Multipart body does not contain 2 or 3 parts"

$scope.upload = function() {

        //storage reference
              var storage = firebase.storage();

              //path reference
              var storageRef = storage.ref();
              var uploadTask = storageRef.child('testfile.png').putString("any string").then(function(snapshot) {
                          console.log('upload successful');
                          alert('ok');
                        }, function (error) {
                          // Handle unsuccessful uploads
                          //alert(error.message);
                   alert(error.serverResponse);
                        });

回答1:

The function added below can help you to add an image on firebase where(file is the image you wish to add ).

$scope.addImage = function(file){
  var fileRef = storageRef.child(file.name);
     fileRef.put(file).then(function (snapshot) {
          console.log(snapshot)
       });
};