Firebase Storage invalid argument on upload

cerealex picture cerealex · Aug 20, 2016 · Viewed 13k times · Source

I keep getting an Invalid argument in put at index 0: Expected Blob or File error. The funny thing is the argument is totally a file...

Here is the code:

var file = document.getElementById('cke_69_fileInput')
          .contentWindow.document.getElementById('cke_69_fileInput_input').files[0];

var storageUrl = 'noticias/imagenes/';
var storageRef = firebase.storage().ref(storageUrl + file.name);
console.warn(file); // Watch Screenshot
var uploadTask = storageRef.put(file);

Here's the screenshot of the actual file warn just before the error asking for a file... enter image description here

Answer

Aaron Saunders picture Aaron Saunders · Aug 21, 2016

try converting file to blob...

  var reader = new FileReader();
  reader.onloadend = function (evt) {
    var blob = new Blob([evt.target.result], { type: "image/jpeg" });

    var storageUrl = 'noticias/imagenes/';
    var storageRef = firebase.storage().ref(storageUrl + file.name);
    console.warn(file); // Watch Screenshot
    var uploadTask = storageRef.put(blob);

  }

  reader.onerror = function (e) {
      console.log("Failed file read: " + e.toString());
  };
  reader.readAsArrayBuffer(file);