UNPKG

6.81 kBMarkdownView Raw
1
2# react-native-file-viewer
3Native file viewer for react-native. Preview any type of file supported by the mobile device.
4
5**iOS**: it uses [QuickLook Framework](https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/UsingtheQuickLookFramework.html)
6
7**Android**: it uses `ACTION_VIEW` Intent to start the default app associated with the specified file.
8
9**Windows**: Start the default app associated with the specified file.
10
11## Getting started
12
13`$ npm install react-native-file-viewer --save`
14
15or
16
17`$ yarn add react-native-file-viewer`
18
19### Mostly automatic installation
20
21`$ react-native link react-native-file-viewer`
22
23### Manual installation
24
25
26#### iOS (CocoaPods)
27
28Add the following to you Podfile:
29```
30pod 'RNFileViewer', :path => '../node_modules/react-native-file-viewer/ios`
31```
32
33#### iOS
34
351. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
362. Go to `node_modules``react-native-file-viewer` and add `RNFileViewer.xcodeproj`
373. In XCode, in the project navigator, select your project. Add `libRNFileViewer.a` to your project's `Build Phases``Link Binary With Libraries`
384. Run your project (`Cmd+R`)
39
40#### Android
41
421. Open up `android/app/src/main/java/[...]/MainApplication.java`
43 - Add `import com.vinzscam.reactnativefileviewer.RNFileViewerPackage;` to the imports at the top of the file
44 - Add `new RNFileViewerPackage()` to the list returned by the `getPackages()` method
452. Append the following lines to `android/settings.gradle`:
46
47 ```
48 include ':react-native-file-viewer'
49 project(':react-native-file-viewer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-file-viewer/android')
50 ```
513. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
52 ```
53 compile project(':react-native-file-viewer')
54 ```
554. Copy `android/src/main/res/xml/file_viewer_provider_paths.xml` to your project `res/xml/` directory
565. Add the following lines to `AndroidManifest.xml` between the main `<application></application>` tag:
57
58 ```
59 ...
60 <application>
61 ...
62 <provider
63 android:name="com.vinzscam.reactnativefileviewer.FileProvider"
64 android:authorities="${applicationId}.provider"
65 android:exported="false"
66 android:grantUriPermissions="true">
67 <meta-data
68 android:name="android.support.FILE_PROVIDER_PATHS"
69 android:resource="@xml/file_viewer_provider_paths"
70 />
71 </provider>
72 </application>
73 ....
74 ```
75
76#### Windows
77
78Follow the instructions in the ['Linking Libraries'](https://github.com/Microsoft/react-native-windows/blob/master/docs/LinkingLibrariesWindows.md) documentation on the react-native-windows GitHub repo. For the first step of adding the project to the Visual Studio solution file, the path to the project should be `../node_modules/react-native-file-viewer/windows/RNFileViewer/RNFileViewer.csproj`.
79
80## API
81
82### `open(filepath: string, options?: Object): Promise<void>`
83
84Parameter | Type | Description
85--------- | ---- | -----------
86**filepath** | string | The absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use [react-native-fs constants](https://github.com/itinance/react-native-fs#constants) to determine the absolute path correctly.
87**options** (optional) | Object | Some options to customize the behaviour. See below.
88
89#### Options
90
91Parameter | Type | Description
92--------- | ---- | -----------
93**displayName** (optional) | string | Customize the QuickLook title (iOS only).
94**showOpenWithDialog** (optional) | boolean | If there is more than one app that can open the file, show an *Open With* dialogue box (Android only).
95**showAppsSuggestions** (optional) | boolean | If there is not an installed app that can open the file, open the Play Store with suggested apps (Android only).
96
97## Usage
98
99### Open a local file
100
101```javascript
102import FileViewer from 'react-native-file-viewer';
103
104const path = // absolute-path-to-my-local-file.
105FileViewer.open(path)
106.then(() => {
107 // success
108})
109.catch(error => {
110 // error
111});
112```
113
114### Pick up and open a local file (using [react-native-document-picker](https://github.com/Elyx0/react-native-document-picker))
115
116```javascript
117import FileViewer from 'react-native-file-viewer';
118import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker';
119
120DocumentPicker.show({
121 filetype: [DocumentPickerUtil.allFiles()],
122}, (error, res) => {
123 if(res) {
124 FileViewer.open(res.uri)
125 .then(() => {
126 // success
127 })
128 .catch(_err => {
129 // error
130 });
131 }
132});
133```
134
135### Prompt the user to choose an app to open the file with (if there are multiple installed apps that support the mimetype)
136
137```javascript
138import FileViewer from 'react-native-file-viewer';
139
140const path = // absolute-path-to-my-local-file.
141FileViewer.open(path, { showOpenWithDialog: true })
142.then(() => {
143 // success
144})
145.catch(error => {
146 // error
147});
148```
149
150### Open a file from Android assets folder
151
152Since the library works only with absolute paths and Android assets folder doesn't have any absolute path, the file needs to be copied first. Use [copyFileAssets](https://github.com/itinance/react-native-fs#copyfileassetsfilepath-string-destpath-string-promisevoid) of [react-native-fs](https://github.com/itinance/react-native-fs).
153
154Example (using react-native-fs):
155
156```javascript
157import FileViewer from 'react-native-file-viewer';
158import RNFS from 'react-native-fs';
159
160const file = 'file-to-open.doc'; // this is your file name
161
162// feel free to change main path according to your requirements
163const dest = `${RNFS.DocumentDirectoryPath}/${file}`;
164
165RNFS.copyFileAssets(file, dest)
166.then(() => FileViewer.open(dest))
167.then(() => {
168 // success
169})
170.catch(error => {
171 /* */
172});
173
174```
175
176### Download and open a file (using [react-native-fs](https://github.com/itinance/react-native-fs))
177No function about file downloading has been implemented in this package.
178Use [react-native-fs](https://github.com/itinance/react-native-fs) or any similar library for this purpose.
179
180Example (using react-native-fs):
181
182```javascript
183import RNFS from 'react-native-fs';
184import FileViewer from 'react-native-file-viewer';
185import { Platform } from 'react-native';
186
187function getLocalPath (url) {
188 const filename = url.split('/').pop();
189 // feel free to change main path according to your requirements
190 return `${RNFS.DocumentDirectoryPath}/${filename}`;
191}
192
193const url = 'https://www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf';
194const localFile = getLocalPath(url);
195
196const options = {
197 fromUrl: url,
198 toFile: localFile
199};
200RNFS.downloadFile(options).promise
201.then(() => FileViewer.open(localFile))
202.then(() => {
203 // success
204})
205.catch(error => {
206 // error
207});
208```