[英]Firebase Storage upload of img dont work -> Invalid argument in `put` at index 0: Expected Blob or File
I try to use firebase storage for the first time.我第一次尝试使用firebase存储。 My goal is to upload img to the storage.我的目标是将 img 上传到存储。
Console shows this err:控制台显示此错误:
"Firebase Storage: Invalid argument in
put
at index 0: Expected Blob or File.", serverResponse_: null, name_: "FirebaseError"} “Firebase 存储:索引 0 处的参数无效:预期的 Blob 或文件。”,serverResponse_:put
,name_:“FirebaseError”}
export default function ImgUpload() {
const [img, setImg] = useState([]);
console.log('img', img);
const handleUpload = () => {
firebase
.storage()
.ref()
.child(`images/${img.name}`)
.put(img)
.then(function (snapshot) {
console.log('what');
});
};
return (
<div>
<Input onChange={setImg} type="file" value={img} />
<Button onClick={e => handleUpload(e)} type="submit">
Upload
</Button>
</div>
);
}
I have also find this in documentation:我也在文档中找到了这个:
var file = ... // use the Blob or File API
ref.put(file).then(function(snapshot) {
console.log('Uploaded a blob or file!'); });
I cant figure out how to set it to work even with this.即使这样,我也不知道如何设置它。
From a quick check of the ReactJS documentation on the file input tag you can get the actual file with this.fileInput.current.files[0]
.通过快速检查文件输入标签上的 ReactJS 文档,您可以使用this.fileInput.current.files[0]
获取实际文件。
So that'd means it'd look something like this for you:所以这意味着它对你来说看起来像这样:
let file = this.fileInput.current.files[0];
firebase
.storage()
.ref()
.child(`images/${file.name}`)
.put(file)
With this in the JSX:在 JSX 中使用这个:
<input type="file" ref={this.fileInput} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.