Effortlessly preview images, videos, and PDFs in your React applications with MyFilePreview. This lightweight and customizable component intelligently detects file types and displays them.
npm install reactjs-file-previewsh
npm install reactjs-file-preview
OR
yarn add reactjs-file-preview
`
---
🚀 Usage
$3
`jsx
import React from "react";
import FilePreview from "reactjs-file-preview";
const App = () => {
return (
File Preview Example
);
};
export default App;
`
$3
`jsx
preview="https://example.com/sample.jpg"
placeHolderImage="https://example.com/placeholder.png"
errorImage="https://example.com/error.png"
/>
`
$3
`jsx
import sampleImg from "../assets/exampleImage.png";
;
`
$3
`jsx
type="file"
onChange={(e) => e.target.files && setFile(e.target.files[0])}
style={{ marginBottom: "1rem" }}
/>
preview={file ?? ""}
placeHolderImage="https://placehold.co/600x400/fff/000?text=Placeholder"
errorImage="https://placehold.co/600x400/fff/FF0000?text=Error"
/>
`
$3
If your files require authentication or are behind CORS restrictions, you can pass a custom Axios instance:
`jsx
import axios from "axios";
const customAxios = axios.create({
headers: {
Authorization: "Bearer YOUR_ACCESS_TOKEN",
},
});
preview="https://example.com/protected-file.pdf"
axiosInstance={customAxios}
/>;
`
---
📂 Supported Formats
reactjs-file-preview currently supports the following file extensions:
- Images: jpg, jpeg, png, gif, webp
- Videos: mp4, webm, ogg
- Documents: pdf
preview | string \| File \| null | ✅ | The source of the file to preview. import sample from "./file.png") null (shows the placeholder if provided) |
clarity | string | ❌ | Defines clarity of the file being previewed. Defaults to 1000. Higher the value, more the loading time of the file. |
placeHolderImage | string | ❌ | URL of an image to display if no file is provided. |
errorImage | string | ❌ | URL of an image to display if the file fails to load. |
fileType | string | ❌ | Type of the file (image, video, pdf). If not provided, the type will be auto-detected. |
axiosInstance | object | ❌ | Custom Axios instance for fetching files, useful for handling authentication or CORS issues. |
preview is not provided (or is null) and a placeHolderImage is supplied, the placeholder image will be shown automatically.