如何在 expo 中使用 XmlHttpRequest 读取 ios 设备中的本地文件? [英] How to read local file in ios devices by using XmlHttpRequest in expo?

查看:29
本文介绍了如何在 expo 中使用 XmlHttpRequest 读取 ios 设备中的本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经处理这个问题 3 天了.事情是这样的,在 iOS 中,我无法使用 XmlHttpRequest 读取文件.我的代码来自世博会的 firebase-storage-upload-example (链接).我已将此代码添加到我的 app.json:

I've been dealing with this problem for 3 days. Here is the thing, in iOS I cannot read file by using XmlHttpRequest. My code is from expo's firebase-storage-upload-example (link). I've added this code to my app.json:

 {
   "expo": {
     ...somestuf
     "ios": {
       "infoPlist": {
          "NSFaceIDUsageDescription": "This app uses the camera to scan barcodes on event tickets.",
          "NSAppTransportSecurity": {
            "NSAllowsArbitraryLoads": true
          }
      }
     }
 }

在 android 中一切正常.在 iOS 中,我认为这是一个许可问题,但我不确定.这是代码和错误:

In android everything works fine. In iOS I think it's a permission kinda issue but I'm not sure. Here is the code and error:

const blob = await new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = () => {
          resolve(xhr.response);
        };
        xhr.onerror = (e) => {
          console.log(e);
          updateStorageLoader({ error: e, loading: false });
          reject(new TypeError('Network Request Failed!'));
        };
        xhr.responseType = 'blob';
        xhr.open('GET', uri, true);
        xhr.send(null);
      });

错误:网络请求失败!.我该如何解决这个问题?FileSystem.readAsStringAsync(fileUri, options) 此方法不是一个选项,因为它不适用于 Firebase 存储上传.

Error: Network Request Failed!. How can I resolve this? FileSystem.readAsStringAsync(fileUri, options) this method is not an option because it's not working with firebase storage upload.

我认为这不是权限问题.我已经下载了最新的 expo firebase 上传文件示例,该文件在新创建的配置为 0 的托管工作流应用程序上运行良好.所以我也愿意接受任何提示而不是直接回答.如果您能指出我将不胜感激的事情.

I think that it's not an issue with permissions. I've downloaded the latest example of expo firebase upload file, which is working totally fine on newly created managed worklflow app with 0 configuration. So I'm open to any hint instead of direct answer as well. Just if you could point out that something I will be appreciated.

推荐答案

使用提到的Buffer包这里在发布到 Firebase 存储时可替代 XMLHttpRequest.

Using the Buffer package mentioned here works as a replacement for XMLHttpRequest when posting to firebase storage.

这是我用来让 with-firebase-storage-upload 用于第二次及后续上传的代码

Here is the code I used to get with-firebase-storage-upload working for 2nd and subsequent uploads

import { Buffer } from "buffer"; // get this via: npm install buffer
import * as FileSystem from "expo-file-system";

...

async function uploadImageAsync(uri) {

    const options = { encoding: FileSystem.EncodingType.Base64 };
    const base64Response = await FileSystem.readAsStringAsync(
        uri,
        options,
    );

    const blob = Buffer.from(base64Response, "base64");

    const ref = firebase
        .storage()
        .ref()
        .child(uuid.v4());
    const snapshot = await ref.put(blob);

    return await snapshot.ref.getDownloadURL();
}

这篇关于如何在 expo 中使用 XmlHttpRequest 读取 ios 设备中的本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆