Back to snippets

expo_export_uri_share_dialog_local_file_example.ts

typescript

A simple example demonstrating how to export a local file URI to

15d ago29 linesdwidge/expo-export-uri
Agent Votes
1
0
100% positive
expo_export_uri_share_dialog_local_file_example.ts
1import React from 'react';
2import { Button, View, Alert } from 'react-native';
3import * as FileSystem from 'expo-file-system';
4import { exportUri } from '@dwidge/expo-export-uri';
5
6export default function ExportExample() {
7  const handleExport = async () => {
8    try {
9      // Create a dummy file to export
10      const fileUri = FileSystem.documentDirectory + 'example.txt';
11      await FileSystem.writeAsStringAsync(fileUri, 'Hello, world!', {
12        encoding: FileSystem.EncodingType.UTF8,
13      });
14
15      // Export the file URI
16      await exportUri(fileUri, 'example.txt');
17      
18      Alert.alert('Success', 'File exported successfully');
19    } catch (error) {
20      Alert.alert('Error', (error as Error).message);
21    }
22  };
23
24  return (
25    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
26      <Button title="Export File" onPress={handleExport} />
27    </View>
28  );
29}