javascript - 共享目录中的文件

标签 javascript reactjs react-native

我有一个可以从外部设备读取值的应用程序。我将这些数据以 .csv 格式保存在下载目录中。

我要做的是在单击它们时共享或打开此文件。

我使用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>
    );
}

此代码显示保存的 .csv 文件的路径。你知道我该怎么做才能打开这个文件,让用户可以选择方法吗?

最佳答案

如果您已正常将文件下载到该路径,并且可以直接在手机上看到下载的文件,则可以使用文件选择器。

  1. npm i --save react-native-document-picker
  2. react-native链接react-native-document-picker

示例

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;
  }
}

如果您需要共享,请创建一个 URL 来下载该文件。

示例

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" />;
  }
}

关于javascript - 共享目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58647419/

相关文章:

javascript - PHP:如果先前已选择该值并将其存储在数据库中,如何从复选框组中删除/禁用选择

react-native - 部分列表不显示

reactjs - Formik + yup : how to validate a location schema where the fields(country, state, city) 不是强制的,但是如果填了一项,是不是每项都应该填?

Javascript/React onClick 向下/向上滚动设置量

reactjs - 更新后出错: Cannot resolve module 'react/lib/ReactMount'

react-native - 如何使用 React-Native-Push-Notification 远程推送通知?

javascript - 如何从数据集的末尾开始加载 Flatlist

javascript - AngularJS Resolve 等待外部图像加载完成

javascript - Jquery 和 PHP 联系表单发送电子邮件

javascript - 在 Canvas 中重新绘制 youtube 缩略图并将它们存储在 localStorage 中