【发布时间】:2023-03-24 20:59:02
【问题描述】:
我正在尝试将图像上传到 Firebase 存储。当我将它们上传到存储时,我看到它们的类型为:application/octet-stream 而不是 image/jpeg 或 image/png。
这是我使用的代码:
<input
type="file"
id="img"
name="filename"
accept="image/*"
onChange={(e) => {
handleChange(e);
}}
/>
const handleChange = async (e) => {
const imgRef = await firebase.storage().ref('images/' + image.name);
await imgRef.put(img);
};
文件被上传到存储中,但文件类型错误。 firebase 存储的文档告诉我们,当您上传没有文件扩展名的文件时,它会自动获取 application/octet-stream 类型。 If no contentType metadata is specified and the file doesn't have a file extension, Cloud Storage defaults to the type application/octet-stream.这真的很奇怪,因为它们以正确的文件扩展名上传到firebase存储。
【问题讨论】:
-
您需要传递带有扩展名的名称或通过元数据传递
contentType。在此处查看此答案:stackoverflow.com/a/54303172/2144912
标签: javascript reactjs firebase firebase-storage file-type