简体   繁体   中英

React file viewer with pdf file

I'm using React-file-viewer, and it works, except for PDF file: This is my code :

 import React, { Component } from 'react'; import FileViewer from 'react-file-viewer' class App extends Component { render() { return ( <FileViewer fileType="pdf" filePath="a.pdf" /> ); } } export default App;
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

And I have this result:

在此处输入图像描述

在此处输入图像描述

I've pdfjs-dist on my dependencies. Do you have an idea?

use require at top like as follows. make sure, file mustnot be outside of src folder.

import React, { Component } from 'react';
import FileViewer from 'react-file-viewer'
const filePath = require(filepath/filename.pdf);

class App extends Component {

  render() {
    return (
      <FileViewer fileType="pdf"
                  filePath={filePath} />
    );
  }

}

export default App;
FileViewer.open(filepath) .then(() =>{ 
console.log('success');
})
        .catch(error =>{ 
          console.log(error);
}

Its working for pdf,video,audio and image file

Is this file viewer works for.docx file or.xls file? Could you please explain how to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM