0

I have an app that read values from an external devices. I save these data in a .csv format in the Download directory.

What I would to do is to share or open this file when they are clicked.

I use react-native FS

componentDidMount(){ this.readingValue() } async readingValue() { // get a list of files and directories in the main bundle :ExternalStorageDirectoryPath const elementiDirectory = await RNFS.readDir(RNFS.ExternalStorageDirectoryPath + '/Download/') const pathElementi = elementiDirectory.map(e => e.path); //console.log("filter " + pathElementi) const pathCSV = pathElementi.filter(path => { const reg = /sd\d+\.csv/; return reg.test(path); }); console.log("risultati " + pathCSV) this.setState({risultati : pathCSV}); } render() { return ( <View> <Text>{this.state.risultati}</Text> </View> ); } 

This code show the path of the .csv file saved. Do you know how can I do to open this file give to the user the possibility to choose the method??

2
  • Can you see the downloaded file directly from your phone? Commented Oct 31, 2019 at 16:23
  • At the moment I can see only the path Commented Oct 31, 2019 at 16:26

1 Answer 1

1

You can use the File Selector if you have downloaded the file normally to the path and can see the downloaded file directly on your mobile phone.

  1. npm i --save react-native-document-picker
  2. react-native link react-native-document-picker

Example

import DocumentPicker from 'react-native-document-picker'; // Pick a single file try { const res = await DocumentPicker.pick({ type: [DocumentPicker.types.images], }); console.log( res.uri, res.type, // mime type res.name, res.size ); } catch (err) { if (DocumentPicker.isCancel(err)) { // User cancelled the picker, exit any dialogs or menus and move on } else { throw err; } } // Pick multiple files try { const results = await DocumentPicker.pickMultiple({ type: [DocumentPicker.types.images], }); for (const res of results) { console.log( res.uri, res.type, // mime type res.name, res.size ); } } catch (err) { if (DocumentPicker.isCancel(err)) { // User cancelled the picker, exit any dialogs or menus and move on } else { throw err; } } 

If you need to share, please create an url to download the file.

Example

import {Share } from 'react-native'; class ShareExample extends Component { onShare = async () => { try { const result = await Share.share({ message: 'csv file download', url: "file:///storage/emulated/0/yourFileName/downfile.csv", }); if (result.action === Share.sharedAction) { if (result.activityType) { // shared with activity type of result.activityType } else { // shared } } else if (result.action === Share.dismissedAction) { // dismissed } } catch (error) { alert(error.message); } }; render() { return <Button onPress={this.onShare} title="Share" />; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I have downloaded the file normally to the path can I use File Selector??
@Jack23 If your phone's internal storage or sdCard has data, you can use it to import it. And I also wrote the answer to share.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.