简体   繁体   English

Firebase 存储上传 img 不起作用 -> 索引 0 处的“put”中的参数无效:预期的 Blob 或文件

[英]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.

相关问题 Firebase 存储:索引 0 处的“put”中的参数无效:预期的 Blob 或文件 - Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File Firebase 错误 Firebase 存储:索引 0 处的 `put` 中的参数无效:预期的 Blob 或文件 - Firebase error Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File Firebase 存储:`put` 预期 Blob 或文件中的参数无效 - Firebase Storage: Invalid argument in `put` Expected Blob or File 文件上传到Firebase存储不起作用(“存储/无效参数”) - file upload to firebase storage is not working (“storage/invalid-argument”) 上传时Firebase存储无效参数 - Firebase Storage invalid argument on upload 错误:尝试将文件上传到 Firebase 存储时出现“无效参数” - Error: 'invalid argument' when trying to upload a file to firebase storage 我收到错误消息“Firebase 存储:`put` 中的参数计数无效:预期在 1 到 2 arguments 之间,收到 0 - i'm getting an error of "Firebase Storage: Invalid argument count in `put`: Expected between 1 and 2 arguments, received 0 Blob URL 上传到 Firebase 存储 - Blob URL upload to firebase storage 如何将 img 正确上传到 firebase 存储(javascript)? - How to upload a img to firebase storage correctly (javascript)? 将文件放入Firebase存储桶 - Put a file in a Firebase Storage Bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM