react-native: Response#arrayBuffer throws `FileReader.readAsArrayBuffer` is not implemented on iOS or android

Description

See title. For example:

      const response = await fetch('https://sheetjs.com/pres.numbers');
      const data = await response.arrayBuffer();

Related issues:

  • #20091 and #21209 were closed and locked because “it does not appear to follow any of the provided issue templates”.
  • #30769 is an inefficient pure-JS attempt to address the situation. The ideal solution would not involve the intermediate Base64 step.

Version

0.69 (latest)

Output of npx react-native info

n/a (reproducible in Expo Snack)

Steps to reproduce

Load https://snack.expo.dev/DsZd8Av6-?platform=android and test in the “iOS” and “android” platforms. The demo does work in the web platform, in case you want to see the expected output.

Snack, code example, screenshot, or link to a repository

https://snack.expo.dev/DsZd8Av6-?platform=android

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 21 (3 by maintainers)

Commits related to this issue

Most upvoted comments

It’s crazy that this isn’t supported. I’ve been trying to find a way around this all day.

Not sure what their reasoning is for not approving the PR?

Not stale.

Still an issue in the latest version (0.70.6), but the offending code is in JS and can be effortlessly patched.

The changes from #30769 work when applied to node_modules/react-native/Libraries/Blob/FileReader.js . For CI use, patch-package can automate this process.

react-native+0.70.6.patch

Download the linked patch file and save it to patches/react-native+0.70.6.patch within your project and run npx patch-package.

This can be verified on macOS using the original demo app:

### new project
npx react-native init AwesomeProject
cd AwesomeProject
### install patch
mkdir -p patches
curl -L -o patches/react-native+0.70.6.patch https://github.com/facebook/react-native/files/10356761/react-native%2B0.70.6.patch
npx patch-package
### use App.js from example
curl -LO https://exp.host/--/api/v2/snack/download/DsZd8Av6-
unzip -p DsZd8Av6- fetch-example/App.js > App.js 
npm i --save https://cdn.sheetjs.com/xlsx-0.19.1/xlsx-0.19.1.tgz
### test
npx react-native run-ios

@Slapbox please test this in your application.

will react native get ArrayBuffer support???

We really can’t upload images in react-native, huh?

You can do it; you just have to spend days of your life searching for a workaround to do what you’d expect to do in a single line of code.

I guess wget magic only gets you so far. I really need this to work so I’ll post any solution I find. Godspeed, everyone.

Expo does not hide the react-native dependency. From scratch:

npx create-expo-app AwesomeProject
cd AwesomeProject 
npm run ios

This runs in the iOS simulator.

Replace App.js with:

import React from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';

export default function App() {
  const [text, setText] = React.useState(0)
  React.useEffect(() => { (async() => {
    try {
      const res = await fetch('https://sheetjs.com/pres.numbers');
      const ab = await res.arrayBuffer();
      setText(`${ab.byteLength} bytes fetched`) ;
    } catch(e) { setText(e?.message || e);}
  })();}, []);
  return (
    <View style={styles.container}>
      <Text>{text}</Text>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

You will see the error “FileReader.readAsArrayBuffer is not implemented”

Testing today, the package.json dependencies include expo 48.0.4 and react-native 0.71.3. The patch for that version is: react-native+0.71.3.patch

To use this:

curl -LO https://github.com/facebook/react-native/files/10857714/react-native%2B0.71.3.patch
mkdir -p patches
mv react-native%2B0.71.3.patch patches/react-native+0.71.3.patch
npx patch-package

That should patch react-native and you should see “652655 bytes fetched”