- Published
- Author
- Sudeep Hipparge
File Preview in React — Beyond Just Images
Building a file preview system in React can surface several helpful patterns — especially when working with images, PDFs, media, and plain text files.
• You can preview any local file before upload using:
It works seamlessly with:
• Images
• PDFs
• Audio files
• Videos
• Plain text
Tip: Always call
🔄 Handling Local and Server Files Together
To support previews for both uploaded and already-stored files, use a union type:
Then render conditionally based on
•
•
•
#CCT1JMA0Z #react
Building a file preview system in React can surface several helpful patterns — especially when working with images, PDFs, media, and plain text files.
• You can preview any local file before upload using:
Code
URL.createObjectURL(file)It works seamlessly with:
• Images
• PDFs
• Audio files
• Videos
• Plain text
Tip: Always call
URL.revokeObjectURL() when the preview is no longer needed to prevent memory leaks.🔄 Handling Local and Server Files Together
To support previews for both uploaded and already-stored files, use a union type:
Code
// Local preview
{ isLocal: true, file: File }
// Server-hosted file
{ url: '/api/files/:id', name: string, type: string }Then render conditionally based on
file.type. Rendering Previews Based on File Type•
<img> → for images•
<iframe> → for PDFs or text files•
<audio> / <video> → for media files#CCT1JMA0Z #react