【问题标题】:Uploading image to Firebase, got an error saying expected blob or file将图像上传到 Firebase,出现错误,提示预期 blob 或文件
【发布时间】:2018-03-14 23:40:23
【问题描述】:

问题

我正在尝试创建一个社交媒体应用程序,用户可以在其中上传图像。我正在为应用程序使用 firebase 和 firebase 存储。我尝试将图像上传到 Firebase,但出现错误:

Firebase Storage: Invalid argument in 'put' at index 0: 
Expected Blob or File

我要上传的图像数据不能是 blob,所以我的问题是,我该如何修复此代码,或者如何以其他方式上传图像?

代码

//uri is the path on the phone to the image
uploadImage(uri) {

 var ref = firebase.storage().ref().child('image.jpg');

  let uriParts = uri.split('.');
  let fileType = uri[uri.length - 1];

  let formData = new FormData();
  formData.append('photo', {
    uri,
    name: `photo.${fileType}`,
    type: `image/${fileType}`,
  });

ref.put(formData).then(function(snapshot) {
  console.log('Uploaded a blob or file!');
});

  };

【问题讨论】:

    标签: firebase react-native firebase-storage


    【解决方案1】:

    要上传新文件,您应该直接使用ref.put(file) 进行更新,文件为 File、Blob 或 Uint8Array。

    See the documentation.

    您可能想使用react-native-fs 获取 Blob,然后上传:

    uploadImage(uri) {
        RNFS.readFile(uri).then(file => {
            // Get Storage ref
            const ref = firebase.storage().ref().child('image.jpg');
    
            const uriParts = uri.split('.');
            const fileType = uri[uri.length - 1];
    
            const metadata = {
                customMetadata: {
                    name: `photo.${fileType}`,
                    type: `image/${fileType}`
                }
            };
    
            ref.put(file, metadata).then(snapshot => {
                console.log('Uploaded a blob or file!');
            });
        });
    };
    

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2019-11-15
      • 2018-01-06
      • 1970-01-01
      • 2016-12-05
      • 2019-10-05
      • 1970-01-01
      • 2013-10-15
      • 2016-12-19
      相关资源
      最近更新 更多