尝试上载到存储时出现firebase错误

时间:2016-09-03 00:08:00

标签: javascript firebase firebase-storage

我在尝试上传到firebase时在控制台中收到下一个错误:

Uncaught FirebaseError: Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.

JS:

var file, fileName, fileRef, storageImagesRef, storageRef, uploadTask;
var storageFolder = '/pictures/';

function getFileName(fileInput) {
file = fileInput.files;
fileName = file[0].name;
storageRef = firebase.storage().ref(storageFolder + fileName);
uploadTask = storageRef.put(file);

};

HTML

<input type="file" onchange="getFileName(this)" accept=".png, .jpg, .bmp, .JPEG, .JPG, .svg, .tiff, .gif">

我做错了什么?

1 个答案:

答案 0 :(得分:3)

问题是由于代码中使用了file变量的方式。变量被赋予FileList - 这是您传递给put的内容。如果您为其分配了File

,那就不那么容易混淆了
function getFileName(fileInput) {

    file = fileInput.files[0];
    fileName = file.name;
    storageRef = firebase.storage().ref(storageFolder + fileName);
    uploadTask = storageRef.put(file);
};