React Native Expo:Base64图像未使用任何上传方法上传到Firebase存储

发布于 2025-02-13 20:37:31 字数 2574 浏览 0 评论 0原文

我正在尝试使用React Native Expo上传Base64图像到Firebase存储。我上传的图像通过

quality:1

base64:true

exif:false 

接下来,我将此图像存储在状态下可变,命名照片,并使用带有URI的映像标签在应用中显示给用户:

data:image/jpg;base64" + photo.base64

现在,当上传它时,我首先根据firebase存储文档如下

uploadString(storageRef, photo.base64, 'base64').then((snapshot) => {
  console.log('Uploaded a raw string!');
});

doc/storage/web/upload-files“ rel

com / 拒绝:firebaseError:firebase存储:字符串不匹配格式'base64':找到无效的字符(storage/novelid-format),

我尝试使用其他参数而不是base64(“ data_url”,“ base64url”,并且不在参数),但是我对“ data_url”和“ base64url”的错误基本相同,当我不将任何内容放在应用程序中时崩溃。此后,我在网上查找以找到修复程序,还有一个人提出的解释此错误消息的问题是,分别使用ATOB和BTOA方法来解码和编码Base64字符串Firebase Storage,但这在JavaScript中不起作用/被弃用。为此,建议的修复程序将其添加到app.js的顶部,

import {decode, encode} from 'base-64'; 
if (!global.btoa) { 
   global.btoa = encode; 
} 
if (!global.atob) 
{ 
   global.atob = decode;
}

但是,当我按下按钮后,应用程序崩溃了,以触发上传串方法。

之后,我尝试根据文档使用上传by方法。我首先尝试传递URI字符串(“数据:image/jpg; base64,base64 data)作为输入,如下所示。虽然这确实使图像在我的firebase存储中可见,但无法打开图像。我认为这是因为这是因为此方法期望一个文件或一个斑点作为输入,我给出的输入是基本64字符串

uploadBytes(storageRef, uri).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

,我在线寻找将基本64字符串转换为blob的方法。 URI和BLOB函数获取BLOB,然后将其放入上传Bytbytes函数中

const response = await fetch(uri)
const blob = await response.blob() 
uploadBytes(storageRef, blob).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

。 Error: ENOENT: no such file or directory, open pathToReactNativeApp/http:/LocalIPAddress:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false'

Next,我尝试了以下内容以获取斑点,如所建议的在这里,但是这会导致应用程序崩溃。

const blob = await new Promise((resolve, reject) => {
             const xhr = new XMLHttpRequest();
             xhr.onload = function () {
             resolve(xhr.response);
          };
             xhr.onerror = function (e) {
             console.log(e);
             reject(new TypeError("Network request failed"));
          };
          // on complete
          xhr.responseType = "blob";
          xhr.open("GET", uri, true);
          xhr.send(null);
     });

有人可以指导我如何将此base64图像上传到firebase存储吗?任何帮助将不胜感激!

I am trying to upload a base64 image to Firebase Storage using React Native Expo. The image I am uploading is taken through the takePictureAsync method in expo-camera library with the following CameraPictureOptions

quality:1

base64:true

exif:false 

Next, I store this image in a state variable, named photo, and display it to the user in the app using the Image tag with the uri:

data:image/jpg;base64" + photo.base64

Now when uploading it I first tried the uploadString method as per the firebase storage documentation as below

uploadString(storageRef, photo.base64, 'base64').then((snapshot) => {
  console.log('Uploaded a raw string!');
});

But it gave the error message

Unhandled promise rejection: FirebaseError: Firebase Storage: String does not match format 'base64': Invalid character found (storage/invalid-format)

I tried this with other parameters instead of base64 ("data_url", "base64url", and not putting anything in the parameter), but I got essentially the same error for "data_url" and "base64url", and when I don't put anything in the parameter the app crashes. After this I looked online to find a fix, and one issue that that some people brought up to explain this error message was that to decode and encode the base64 string firebase storage used the atob and btoa method respectively, but this did not work in javascript/was deprecated. To that end, the fix that was suggested was adding this to on top of App.js

import {decode, encode} from 'base-64'; 
if (!global.btoa) { 
   global.btoa = encode; 
} 
if (!global.atob) 
{ 
   global.atob = decode;
}

However, when I did this the app crashed after the button was pressed that triggered the uploadString method.

After this, I tried using the uploadBytes method as per the documentation. I first tried passing the uri string ("data:image/jpg;base64, base64 data) as input, as below. While this did make the image visible on my firebase storage, the image could not be opened. I think this was because this method expected a File or a Blob as input and the input I gave was a base64 string

uploadBytes(storageRef, uri).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

Next, I looked online for ways to convert the base64 string to a Blob. The first approach I tried was calling the fetch api on uri and the blob function to get the blob and then put that in the uploadBytes function.

const response = await fetch(uri)
const blob = await response.blob() 
uploadBytes(storageRef, blob).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

However, I get the following error message, which I was unable to solve.
Error: ENOENT: no such file or directory, open pathToReactNativeApp/http:/LocalIPAddress:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false'

Next, I tried the following to get the blob as suggested here, but this lead the app to crash.

const blob = await new Promise((resolve, reject) => {
             const xhr = new XMLHttpRequest();
             xhr.onload = function () {
             resolve(xhr.response);
          };
             xhr.onerror = function (e) {
             console.log(e);
             reject(new TypeError("Network request failed"));
          };
          // on complete
          xhr.responseType = "blob";
          xhr.open("GET", uri, true);
          xhr.send(null);
     });

Can someone please guide me on how I can upload this base64 image to firebase storage? Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2025-02-20 20:37:32
    xhr.onerror = reject;
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        resolve(xhr.response);
      }
    };
    xhr.open("GET", signature);
    xhr.responseType = "blob";
    xhr.send();

尝试一下。遇到了同样的问题,但这有帮助。
解决方案发布在这里

    xhr.onerror = reject;
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        resolve(xhr.response);
      }
    };
    xhr.open("GET", signature);
    xhr.responseType = "blob";
    xhr.send();

Try this. Been having the same issues but this helped.
Solution was posted here.

甜味超标? 2025-02-20 20:37:32

来自Expo的ImagePicker.launchimagelibraryAsync将返回URI(不需要包含base64:true)

使用该URI生成Blob

Const响应=等待fetch(result.uri);

const blob =等待响应.blob();

使用此BLOB,使用上传Bybytes函数上载

Bobtes(Ref,Blob).....

IM使用Firebase 9 Web Moduler,将在所有平台上

运行的blob远比使用Base64和快速的Aswell在所有平台上运行。

不仅图像,您还可以上传视频。

ImagePicker.launchImageLibraryAsync from expo, will return uri, (no need to include base64 :true)

using that uri generate blob

const response = await fetch(result.uri);

const blob = await response.blob();

using this blob, upload to firebase storage using uploadBytes function

uploadBytes(ref, blob).....

i m using firebase 9 web moduler, will run in all platforms

uploading blob is far batter than using base64 and speedy aswell.

not only images, you can upload videos aswell with this,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文