How to get file from URI | Expo | React Native

隐身守侯 提交于 2019-12-07 02:39:48

问题


I have ejected project of the expo.

After changing info.plist, now I am able to get my app in the list of "open with app list" and actually able to open that file with my expo(React native app).

App.js

Linking.getInitialURL().then((url) => {
  if (url) {
        console.log(url);
    }

}).catch(err => console.error('An error occurred', err));

this code is giving me this URL.

file:///private/var/mobile/Containers/Data/Application/7E55EB55-7C49-4C0C-B4CB-63AC4F49689E/Documents/Inbox/matters-3.csv

So, that means now I have URL of the email attachment, but How am I able to get the data of that csv string in my app?

So I am assuming, when I click open with my app. The URL that is passed into my app from the system is actually a copy of the document that is placed somewhere in our app’s directory.

But when I trying to access this file with Expo.FileSystem. readAsStringAsync it's giving me an error says, the file is not readable.

is there anything to do with storage permission?

Need Help....?


回答1:


I think you could use react-native-fs. This here should work and print out the contents of the CSV file to the console.

App.js

var RNFS = require('react-native-fs');

Linking.getInitialURL().then((url) => {
  if (url) {
    RNFS.readFile(url).then((data) => {
      console.log(data);
    }

}).catch(err => console.error('An error occurred', err));



回答2:


You can use react-native-fs as Carlo mentioned or rn-fetch-blob, I recommend rn-fetch-blob, to read a file u can check their documentation, it goes something like this

let data = '';
RNFetchBlob.fs.readStream( ...options).then((ifstream) => {
ifstream.open()
ifstream.onData((chunk) => {
  data += chunk
  // show progress ...%
})
ifstream.onError((err) => {
  console.log('oops', err)
})
ifstream.onEnd(() => {  
  consol.log('final data', data)
})

}))



来源:https://stackoverflow.com/questions/53160752/how-to-get-file-from-uri-expo-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!