Firebase Storage invalid argument on upload

14,373

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);
Share:
14,373
cerealex
Author by

cerealex

Updated on June 08, 2022

Comments

  • cerealex
    cerealex about 2 years

    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

  • Arjun Ajith
    Arjun Ajith about 7 years
    Put {type:mime} for uploading files of all formats.
  • vir us
    vir us about 6 years
    @Aaron Saunders - thanks! And while blob works I'm wondering what firebase expects as a "file" in this case?
  • vir us
    vir us about 6 years
    one can also use {type:file.type} to specify mime type