Socket
Socket
Sign inDemoInstall

react-native-file-viewer

Package Overview
Dependencies
514
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-file-viewer

Native file viewer for react-native


Version published
Weekly downloads
83K
decreased by-0.32%
Maintainers
1
Install size
44.8 kB
Created
Weekly downloads
 

Readme

Source

react-native-file-viewer

Native file viewer for react-native. Preview any type of file supported by the mobile device.

iOS: it uses QuickLook Framework

Android: it uses ACTION_VIEW Intent to start the default app associated with the specified file.

Windows: Start the default app associated with the specified file.

Getting started

$ npm install react-native-file-viewer --save

or

$ yarn add react-native-file-viewer

Mostly automatic installation

$ react-native link react-native-file-viewer

Manual installation

iOS
  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-file-viewer and add RNFileViewer.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNFileViewer.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)
Android
  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.vinzscam.reactnativefileviewer.RNFileViewerPackage; to the imports at the top of the file
  • Add new RNFileViewerPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:

    include ':react-native-file-viewer'
    project(':react-native-file-viewer').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-file-viewer/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:

      compile project(':react-native-file-viewer')
    
  3. Copy android/src/main/res/xml/file_viewer_provider_paths.xml to your project res/xml/ directory

  4. Add the following lines to AndroidManifest.xml between the main <application></application> tag:

    ...
    <application>
    ...
    	<provider
    		android:name="com.vinzscam.reactnativefileviewer.FileProvider"
    		android:authorities="${applicationId}.provider"
    		android:exported="false"
    		android:grantUriPermissions="true">
    		<meta-data
    			android:name="android.support.FILE_PROVIDER_PATHS"
    			android:resource="@xml/file_viewer_provider_paths"
    		/>
    	</provider>
    </application>
    ....
    
Windows

Follow the instructions in the 'Linking Libraries' 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.

API

open(filepath: string, options?: Object): Promise<void>

ParameterTypeDescription
filepathstringThe absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use react-native-fs constants to determine the absolute path correctly.
options (optional)ObjectSome options to customize the behaviour. See below.
Options
ParameterTypeDescription
displayName (optional)stringCustomize the QuickLook title (iOS only).
showOpenWithDialog (optional)booleanIf there is more than one app that can open the file, show an Open With dialogue box (Android only).
showAppsSuggestions (optional)booleanIf there is not an installed app that can open the file, open the Play Store with suggested apps (Android only).

Usage

Open a local file

import FileViewer from 'react-native-file-viewer';

const path = // absolute-path-to-my-local-file.
FileViewer.open(path)
.then(() => {
	// success
})
.catch(error => {
	// error
});

Prompt the user to choose an app to open the file with (if there are multiple installed apps that support the mimetype)

import FileViewer from 'react-native-file-viewer';

const path = // absolute-path-to-my-local-file.
FileViewer.open(path, { showOpenWithDialog: true })
.then(() => {
	// success
})
.catch(error => {
	// error
});

Open a file from Android assets folder

Since 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 of react-native-fs.

Example (using react-native-fs):

import FileViewer from 'react-native-file-viewer';
import RNFS from 'react-native-fs';

const file = 'file-to-open.doc'; // this is your file name

// feel free to change main path according to your requirements
const dest = `${RNFS.DocumentDirectoryPath}/${file}`;

RNFS.copyFileAssets(file, dest)
.then(() => FileViewer.open(dest))
.then(() => {
   // success
})
.catch(_err => {
   /* */
});

Download and open a file (using react-native-fs)

No function about file downloading has been implemented in this package. Use react-native-fs or any similar library for this purpose.

Example (using react-native-fs):

import RNFS from 'react-native-fs';
import FileViewer from 'react-native-file-viewer';
import { Platform } from 'react-native';

function getLocalPath (url) {
  const filename = url.split('/').pop();
  // feel free to change main path according to your requirements
  return `${RNFS.DocumentDirectoryPath}/${filename}`;
}

const url = 'https://www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf';
const localFile = getLocalPath(url);

const options = {
  fromUrl: url,
  toFile: localFile
};
RNFS.downloadFile(options).promise
.then(() => FileViewer.open(localFile))
.then(() => {
	// success
})
.catch(error => {
	// error
});

Keywords

FAQs

Last updated on 26 Oct 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc