Introduction
react-file-viewer is a powerful plugin written in React.js to open pdf, png, docx, csv, mp3, mp4 and xlsx files.
Open a command prompt. Create a directory for the SPFx solution.
md spfx-ReactFileViewer
Navigate to the above-created directory.
cd spfx-ReactFileViewer
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have the default name (spfx-ReactFileViewerin this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - ReactFileViewer
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
- npm i react-file-viewer
- npm i csstype
- import FileViewer from 'react-file-viewer';
- import * as CSS from 'csstype';
Full Code
in ReactFileViewer.tsx
- import * as React from 'react';
- import { IReactFileViewerProps } from './IReactFileViewerProps';
- import FileViewer from 'react-file-viewer';
- import * as CSS from 'csstype';
- let myfile:string = '';
- let mytype:string = '';
- interface IPnpstate {
- file:string;
- type:string;
- }
- var divStyle: CSS.Properties = {
- minHeight:'auto'
- };
- export default class ReactFileViewer extends React.Component<IReactFileViewerProps, IPnpstate> {
- // private recaptchaRef = React.createRef<HTMLInputElement>();
- constructor(props: IReactFileViewerProps, state: IPnpstate) {
- super(props);
- this.state = {
- file:'/Shared%20Documents/Total_Crime.csv',
- type :'csv'
- };
- }
- public componentDidMount() {
- (document.querySelector("#FileViewer") as HTMLOptionElement).addEventListener('change', (e) =>{
- let mytargetoption = (e.target as HTMLTextAreaElement).value;
- switch (mytargetoption) {
- case 'pdf':
- myfile="/Shared%20Documents/sample.pdf";
- mytype="pdf";
- break;
- case 'csv':
- myfile="/Shared%20Documents/Total_Crime.csv";
- mytype="csv";
- break;
- case 'png':
- myfile="/Shared%20Documents/teams.png";
- mytype="png";
- break;
- case 'xlsx':
- myfile="/Shared%20Documents/Financial Sample.xlsx";
- mytype="xlsx";
- break;
- case 'docx':
- myfile="/Shared%20Documents/11KB.docx";
- mytype="docx";
- break;
- default:
- console.log('file fromat not found');
- }
- this.setState({ file: myfile,type: mytype });
- }
- //,{ once: true }
- );
- }
- public render(): React.ReactElement<IReactFileViewerProps> {
- return (
- <div style={divStyle}>
- <select id="FileViewer">
- <option value="pdf">PDF</option>
- <option value="xlsx">XLSX</option>
- <option value="png">PNG</option>
- <option value="docx">DOCX</option>
- <option value="csv">CSV</option>
- </select>
- <FileViewer
- fileType={this.state.type}
- filePath={this.state.file}
- />
- </div>
- );
- }
- }
Properties
- fileType-type of the file where pdf,png,mp3 etc(Mandatory)
- filepath-path of the file (where cors is not supported) (Mandatory)
- onError- function that will be called when there is an error in the file viewer fetching or rendering the requested resource. (Optional)
- errorComponent- A component to render in case of error instead of the default error component that comes packaged with react-file-viewer.(Optional)
- unsupportedComponent - A component to render in case the file format is not supported. (Optional)
The Where plugin has some size issue in image,xlsx,csv format in current version. I hope it will be solved in the next release.
Expected Output


Conclusion
Hence we learned how to implement fileviewer in spfx using react plugin react-file-viewer. I hope this helps someone. Happy coding :)

Join the conversation! Your thoughts help the community grow.