upd: dowload file
This commit is contained in:
@@ -56,12 +56,6 @@ export default function RootLayout() {
|
||||
headerTitleAlign: 'center',
|
||||
headerRight: () => <HeaderRightDivisionList />
|
||||
}} />
|
||||
<Stack.Screen name="coba" options={{
|
||||
headerLeft: () => <ButtonBackHeader onPress={() => { router.push('/home') }} />,
|
||||
title: 'Latihan tab',
|
||||
headerTitleAlign: 'center',
|
||||
headerRight: () => <HeaderMemberList />
|
||||
}} />
|
||||
<Stack.Screen name="division/[id]/(fitur-division)" options={{ headerShown: false }} />
|
||||
<Stack.Screen name="group/index" options={{
|
||||
headerLeft: () => <ButtonBackHeader onPress={() => { router.back() }} />,
|
||||
|
||||
@@ -3,12 +3,11 @@ import { apiDeleteFileProject, apiGetProjectOne } from "@/lib/api";
|
||||
import { setUpdateProject } from "@/lib/projectUpdate";
|
||||
import { useAuthSession } from "@/providers/AuthProvider";
|
||||
import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons";
|
||||
import * as FileSystem from 'expo-file-system';
|
||||
// import * as FileSystem from 'expo-file-system';
|
||||
import { Directory, File, Paths } from 'expo-file-system/next';
|
||||
import * as MediaLibrary from 'expo-media-library';
|
||||
import { useLocalSearchParams } from "expo-router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Alert, Platform, Text, ToastAndroid, View } from "react-native";
|
||||
import { Text, ToastAndroid, View } from "react-native";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import AlertKonfirmasi from "../alertKonfirmasi";
|
||||
import BorderBottomItem from "../borderBottomItem";
|
||||
@@ -80,72 +79,16 @@ export default function SectionFile({ status, member }: { status: number | undef
|
||||
}
|
||||
}
|
||||
|
||||
const downloadFile = async (fileUrl: string) => {
|
||||
try {
|
||||
// Get file name from URL
|
||||
const fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
|
||||
|
||||
// Create local file path
|
||||
const fileUri = `${FileSystem.documentDirectory}${fileName}`;
|
||||
|
||||
// Download the file with progress tracking
|
||||
const downloadResumable = FileSystem.createDownloadResumable(
|
||||
fileUrl,
|
||||
fileUri,
|
||||
{},
|
||||
(downloadProgress) => {
|
||||
const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
|
||||
}
|
||||
);
|
||||
const downloadResult = await downloadResumable.downloadAsync();
|
||||
|
||||
if (downloadResult && downloadResult.uri) {
|
||||
const uri = downloadResult.uri;
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
// For Android, we need to save to media library
|
||||
const { status } = await MediaLibrary.requestPermissionsAsync();
|
||||
|
||||
if (status === 'granted') {
|
||||
const fileString = await FileSystem.readAsStringAsync(uri, { encoding: FileSystem.EncodingType.Base64 });
|
||||
const newFile = await FileSystem.StorageAccessFramework.createFileAsync(
|
||||
uri,
|
||||
'sample',
|
||||
downloadResult.headers['content-type']
|
||||
);
|
||||
await FileSystem.writeAsStringAsync(newFile, fileString, { encoding: FileSystem.EncodingType.Base64 });
|
||||
Alert.alert('Success', `File downloaded and saved to Downloads folder`);
|
||||
} else {
|
||||
Alert.alert('Error', 'Permission to access media library denied');
|
||||
}
|
||||
} else {
|
||||
// For iOS, the file is already saved to the app's document directory
|
||||
Alert.alert('Success', `File downloaded and saved to app's documents folder`);
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error downloading file:', error);
|
||||
Alert.alert('Error', `Failed to download file: ${error}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
async function download() {
|
||||
const url = 'https://pdfobject.com/pdf/sample.pdf';
|
||||
const destination = new Directory(Paths.cache, 'pdfs');
|
||||
try {
|
||||
destination.create();
|
||||
const output = await File.downloadFileAsync(url, destination);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
const destination = new Directory(Paths.document, 'pdfs');
|
||||
const filename = "dummy.pdf";
|
||||
const result = await File.downloadFileAsync('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf', destination);
|
||||
console.log(result)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<View style={[Styles.mb15]}>
|
||||
@@ -192,8 +135,8 @@ export default function SectionFile({ status, member }: { status: number | undef
|
||||
icon={<MaterialCommunityIcons name="download" color="black" size={25} />}
|
||||
title="Download"
|
||||
onPress={() => {
|
||||
downloadFile('https://pdfobject.com/pdf/sample.pdf')
|
||||
setModal(false)
|
||||
download()
|
||||
// setModal(false)
|
||||
}}
|
||||
/>
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"dependencies": {
|
||||
"@expo/vector-icons": "^14.0.2",
|
||||
"@react-native-async-storage/async-storage": "^2.1.2",
|
||||
"@react-native-community/datetimepicker": "8.3.0",
|
||||
"@react-native-community/datetimepicker": "8.2.0",
|
||||
"@react-navigation/bottom-tabs": "^7.2.0",
|
||||
"@react-navigation/native": "^7.0.14",
|
||||
"@reduxjs/toolkit": "^2.7.0",
|
||||
@@ -40,6 +40,7 @@
|
||||
"expo-media-library": "~17.1.6",
|
||||
"expo-permissions": "^14.4.0",
|
||||
"expo-router": "~5.0.7",
|
||||
"expo-sharing": "^13.1.5",
|
||||
"expo-splash-screen": "~0.30.8",
|
||||
"expo-status-bar": "~2.2.3",
|
||||
"expo-symbols": "~0.4.4",
|
||||
|
||||
Reference in New Issue
Block a user