【问题标题】:Firebase storage file type is 'octet-stream' instead of 'png/jpg'Firebase 存储文件类型是 'octet-stream' 而不是 'png/jpg'
【发布时间】:2023-03-24 20:59:02
【问题描述】:

我正在尝试将图像上传到 Firebase 存储。当我将它们上传到存储时,我看到它们的类型为:application/octet-stream 而不是 image/jpegimage/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存储。

【问题讨论】:

标签: javascript reactjs firebase firebase-storage file-type


【解决方案1】:

可以像这样添加元数据:

storageRef.put(file, {
    contentType: 'image/png',
})

【讨论】:

    【解决方案2】:

    我认为此解决方案将适用于您的情况:

    https://stackoverflow.com/a/54303172

    您需要使用.child() 指定文件扩展名或将内容类型元数据添加到您的上传文件。

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2016-03-13
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多