中文 · English
NGX File Preview is a powerful Angular file preview component library that supports previewing a wide variety of file formats and offers flexible customization options, providing an efficient and user-friendly solution for developers.
File Type | Preview Effect |
---|---|
Image | ![]() ![]() |
Video | ![]() ![]() |
Audio | ![]() ![]() |
PPT | ![]() ![]() |
Word | ![]() ![]() |
Excel | ![]() ![]() |
Text | ![]() ![]() |
Zip | ![]() ![]() |
Unknown | ![]() ![]() |
- Support for Multiple File Formats: Fully compatible with image, PDF, PPT, Word, text, video, and many other common file types.
- Intuitive User Experience: Provides clear indicators for unknown file types and supports user-friendly interactions for different file formats.
- Dark Mode and Light Mode: Adapts to various use cases with visual preferences for both dark and light modes, including auto mode switching.
- Flexible Usage: Supports both directive-based and component-based usage, offering flexibility to meet different development requirements.
- Lightweight Design: Optimized for performance and easy integration into any project, ensuring smooth and efficient operation.
- Keyboard Shortcut Support: Increases efficiency by allowing easy navigation and closing of the preview with keyboard shortcuts.
npm install @eternalheart/ngx-file-preview --save docx-preview hls.js ng2-pdf-viewer pptx-preview xlsx
Add the necessary styles and scripts to your angular.json
file:
{
"projects": {
"your-app": {
"architect": {
"build": {
"options": {
"styles": [
"node_modules/@eternalheart/ngx-file-preview/src/assets/icon/font/nfp.css"
],
"scripts": [
"node_modules/@eternalheart/ngx-file-preview/src/assets/icon/color/nfp.js"
]
}
}
}
}
}
}
Import the required components in your Angular module:
import {
PreviewDirective,
PreviewComponent,
PreviewModalComponent
} from '@eternalheart/ngx-file-preview';
@Component({
imports: [
PreviewDirective,
PreviewComponent,
]
})
The simplest usage method: Apply the directive directly to an element:
import { PreviewDirective, PreviewEvent } from '@eternalheart/ngx-file-preview';
@Component({
imports: [PreviewDirective],
template: `
<!-- Use themeMode to specify the display mode (dark/light). By default, it automatically switches based on time, but can also be manually toggled inside the preview page -->
<div [ngxFilePreview]="file" themeMode="light">Click to preview a single file</div>
<div [ngxFilePreview]="file" themeMode="dark" (previewEvent)="handlePreviewEvent($event)">Click to preview a single file</div>
<div [ngxFilePreview]="file" themeMode="auto" [autoConfig]="{dark: {start: 19, end: 7}}">Click to preview a single file</div>
<div [ngxFilePreview]="files">Click to preview multiple files</div>
`
})
export class YourComponent {
file: PreviewFile = {
name: 'example.jpg',
type: 'image',
url: 'path/to/file.jpg'
};
files: PreviewFile[] = [
// ... file array
];
handlePreviewEvent(event: PreviewEvent) {
const { type, message, event: targetEvent } = event;
if (type === "error") {
console.log(message); // Handle error event
}
if (type === "select") {
console.log(targetEvent); // Handle file selection event
}
}
}
import { PreviewComponent } from '@eternalheart/ngx-file-preview';
@Component({
imports: [PreviewComponent],
template: `
<ngx-file-preview [files]="files" (fileSelect)="onFileSelect($event)">
</ngx-file-preview>
`
})
@Component({
template: `
<ngx-file-preview [files]="files">
<ng-template #itemTemplate
let-file
let-index="index"
let-isActive="isActive"
let-preview="preview">
<div class="custom-item"
[class.active]="isActive"
(click)="preview()">
<span>{{ file.name }}</span>
<span>{{ formatFileSize(file.size) }}</span>
</div>
</ng-template>
</ngx-file-preview>
`
})
interface PreviewFile {
url: string; // File URL
type: PreviewType; // File type
name: string; // File name
size?: number; // File size (optional)
lastModified?: number;// Last modified time (optional)
coverUrl?: string; // Cover image URL (for video/audio, optional)
}
type PreviewType =
| 'image' // Image
| 'pdf' // PDF Document
| 'ppt' // PPT Presentation
| 'word' // Word Document
| 'txt' // Text File
| 'video' // Video
| 'excel' // Excel Spreadsheet
| 'audio' // Audio
| 'zip' // Compressed File
| 'unknown' // Unknown Type
In preview mode, the following shortcuts are available:
←
Previous file→
Next fileEsc
Close preview
- Clone the repository:
git clone https://github.com/wh131462/ngx-file-preview.git
- Install dependencies:
npm install
- Start the development server:
npm run start
- Build the library:
npm run build
Feel free to submit Issues and Pull Requests to help improve this project!
MIT
This project uses the following excellent open-source libraries:
- docx-preview - Word document preview
- pptx-preview - PPT presentation preview
- xlsx - Excel spreadsheet preview
- ng2-pdf-viewer - PDF document preview
- hls.js - HLS video stream support
We appreciate the contributions from these open-source projects to the community!