Gathering detailed insights and metrics for @datacomvn/file-opener
Gathering detailed insights and metrics for @datacomvn/file-opener
Gathering detailed insights and metrics for @datacomvn/file-opener
Gathering detailed insights and metrics for @datacomvn/file-opener
npm install @datacomvn/file-opener
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
24
A high-performance React Native library for opening files with native viewers
Built with ⚡ Nitro Modules for blazing-fast performance
1npm install @datacomvn/file-opener react-native-nitro-modules
1yarn add @datacomvn/file-opener react-native-nitro-modules
1pnpm add @datacomvn/file-opener react-native-nitro-modules
Note:
react-native-nitro-modules
is a required peer dependency as this library is built on Nitro Modules.
No additional setup required. The library uses iOS's native QuickLook framework.
Add the following to your android/app/src/main/AndroidManifest.xml
:
1<provider 2 android:name="androidx.core.content.FileProvider" 3 android:authorities="${applicationId}.fileprovider" 4 android:exported="false" 5 android:grantUriPermissions="true"> 6 <meta-data 7 android:name="android.support.FILE_PROVIDER_PATHS" 8 android:resource="@xml/file_paths" /> 9</provider>
Create android/app/src/main/res/xml/file_paths.xml
:
1<?xml version="1.0" encoding="utf-8"?> 2<paths xmlns:android="http://schemas.android.com/apk/res/android"> 3 <external-files-path name="external_files" path="." /> 4 <external-cache-path name="external_cache" path="." /> 5 <files-path name="files" path="." /> 6 <cache-path name="cache" path="." /> 7</paths>
1import { FileOpener } from '@datacomvn/file-opener'; 2 3// Open a PDF file 4const success = FileOpener.open('/path/to/document.pdf'); 5 6if (success) { 7 console.log('File opened successfully!'); 8} else { 9 console.error('Failed to open file'); 10}
FileOpener.open(path: string): boolean
Opens a file with the system's default application for that file type.
path
(string): Absolute path to the file to be openedboolean
: true
if the file was successfully opened, false
otherwise1import { FileOpener } from '@datacomvn/file-opener'; 2import RNFS from 'react-native-fs'; 3 4// Example: Open a downloaded PDF 5const downloadAndOpen = async () => { 6 try { 7 const downloadDest = `${RNFS.DocumentDirectoryPath}/sample.pdf`; 8 const download = RNFS.downloadFile({ 9 fromUrl: 'https://example.com/sample.pdf', 10 toFile: downloadDest, 11 }); 12 13 const result = await download.promise; 14 15 if (result.statusCode === 200) { 16 const opened = FileOpener.open(downloadDest); 17 18 if (opened) { 19 console.log('PDF opened successfully!'); 20 } else { 21 console.error('Failed to open PDF'); 22 } 23 } 24 } catch (error) { 25 console.error('Download failed:', error); 26 } 27};
The library supports a wide range of file formats:
.pdf
.doc
, .docx
, .xls
, .xlsx
, .ppt
, .pptx
.txt
, .md
, .csv
, .xml
, .html
, .css
, .json
.jpg
, .jpeg
, .png
, .gif
, .bmp
, .webp
.svg
.ico
.mp4
, .mkv
, .webm
, .avi
, .mov
, .wmv
, .flv
.mp3
, .aac
, .wav
, .ogg
, .midi
, .flac
.js
, .ts
, .java
, .cpp
, .c
, .py
, .php
, .rb
, .go
, .swift
, .kt
.zip
, .rar
, .7z
, .tar
, .gz
.epub
.ttf
, .otf
, .woff
, .woff2
.ics
.apk
, .exe
, .dmg
, .iso
1import { FileOpener } from '@datacomvn/file-opener'; 2 3const openFileWithErrorHandling = (filePath: string) => { 4 try { 5 const success = FileOpener.open(filePath); 6 7 if (!success) { 8 // Handle various failure scenarios 9 console.error('Failed to open file. Possible reasons:'); 10 console.error('- File does not exist'); 11 console.error('- No application available to handle this file type'); 12 console.error('- Insufficient permissions'); 13 console.error('- File is corrupted or invalid'); 14 } 15 16 return success; 17 } catch (error) { 18 console.error('Unexpected error:', error); 19 return false; 20 } 21};
1import { FileOpener } from '@datacomvn/file-opener'; 2import RNFS from 'react-native-fs'; 3 4const openFileWithValidation = async (filePath: string) => { 5 try { 6 // Check if file exists 7 const exists = await RNFS.exists(filePath); 8 if (!exists) { 9 throw new Error('File does not exist'); 10 } 11 12 // Get file info 13 const stat = await RNFS.stat(filePath); 14 console.log('File size:', stat.size, 'bytes'); 15 console.log('Modified:', new Date(stat.mtime)); 16 17 // Open the file 18 const success = FileOpener.open(filePath); 19 20 if (success) { 21 console.log('File opened successfully!'); 22 } else { 23 console.error('Failed to open file'); 24 } 25 26 return success; 27 } catch (error) { 28 console.error('Error:', error.message); 29 return false; 30 } 31};
This library is built with Nitro Modules, which provides:
1// Ensure you have the FileProvider configured in AndroidManifest.xml 2// and the file_paths.xml resource file created
1// Make sure your app has the necessary permissions to access the file 2// and that the file is in an accessible location
1// This happens when no app can handle the file type 2// Consider providing a fallback or user guidance
We welcome contributions! Please see our Contributing Guide for details.
yarn install
yarn example ios
or yarn example android
1# Run type checking 2yarn typecheck 3 4# Run linting 5yarn lint 6 7# Run tests 8yarn test
MIT © Datacom Vietnam
This library is developed and maintained by Datacom Vietnam, a leading technology company specializing in innovative mobile and web solutions.
No vulnerabilities found.
No security vulnerabilities found.