XPO中使用XmlHttpRequest读取ios设备中的本地文

XPO中使用XmlHttpRequest读取ios设备中的本地文

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

问题描述

我已经处理了3天了.这就是问题,在iOS中,我无法使用XmlHttpRequest读取文件.我的代码来自expo的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);
      });

错误:Network Request Failed!.我该如何解决? 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.

这是我用于与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设备中的本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:14