Building File Upload and Download Features in Node.js Chat Applications

Welcome
Last time, we implemented the ability to upload images and share them in real-time.
Now users can freely send and receive photos within the chat service.
However, in actual work scenarios, sharing various documents is more common than just photos.
For example, PDFs, compressed files, Excel spreadsheets, and other document types are frequently shared.
In this lesson, we'll implement file upload and download functionality to create a more practical chat service.
Why File Sharing Is Needed
Most modern messaging apps allow sharing various file types.
For example:
PDF documents
Word documents
Excel files
PowerPoint presentations
ZIP compressed files
Text files
These can be sent easily.
This feature is commonly used in business messaging applications.
File Upload Process
The process of sending a file is nearly identical to image uploading.
First, the user selects a file.
The browser uploads the selected file to the server.
Once the upload completes, the server generates a storage location and filename for the file.
Then, using Socket.IO, the server sends the file information to other users in the same chat room.
The browser uses this information to display download links on the screen.
File Upload with Multer
Multer, which was used for image uploading, also supports general file uploads.
With Multer, you can easily handle:
Specifying storage locations
Rename files
Set file size limits
Validate file extensions
This lesson will implement a feature for uploading various file types using Multer.
Allowed File Types
Allowing all files to be uploaded can pose security risks.
Therefore, it's better to limit the allowed file types.
For example:
PDF
DOCX
XLSX
PPTX
TXT
ZIP
Allowing only these commonly used business file types creates a safer service.
File Size Limits
Very large files can quickly consume server storage space.
They also increase upload time and server load.
In practice, file size limits are commonly set to:
10MB
20MB
50MB
This project will also implement a maximum upload size limit for stable service.
Displaying File Information
Users should be able to easily identify what file they're receiving.
For example, displaying:
File name
File size
Upload time
Sent by
Makes the process more convenient.
Real messaging apps also provide this information alongside files.
Implementing Download Functionality
Uploaded files must be downloadable.
The browser sends a request to the server when clicking a download link, and the server transmits the file.
Users can then save the file to their computer for use.
This process is straightforward to implement in Express.
Important Considerations for Downloads
Security is important when implementing download functionality.
For example, you should check that:
The file doesn't exist
The file has been deleted
The file path isn't unauthorized
Downloads shouldn't proceed if these conditions are met.
It's also important to prevent users from accessing internal server files by directly entering file paths.
Displaying File Icons
Displaying different icons based on file type makes it more user-friendly.
For example:
PDF → PDF icon
Word → Document icon
ZIP → Compressed file icon
Excel → Excel icon
This helps users quickly identify the file type.
Potential Future Features
The file sharing feature implemented in this lesson can be expanded with additional capabilities:
Uploading multiple files
Drag-and-drop uploads
Displaying upload progress
Deleting files
Previewing files
Virus scanning
Showing download counts
These additions would create a more polished service.
How This Is Used in Practice
Business messaging apps typically provide file sharing features.
For example:
Sharing work documents
Transferring project materials
Sending contracts
Sharing presentation materials
This feature is used for various purposes in business environments.
Therefore, file upload functionality is one of the most important features in real-time chat services.
Summary of This Lesson
In this session, we learned how to implement uploading and downloading various files to a server using Node.js.
We also explored Multer-based file uploads, displaying file information, and handling downloads.
Now the chat service can share not only text and images but also various documents freely.
Next Lesson Preview
In the next lesson, we'll implement storing chat history in MySQL to save conversation records.
We'll complete a database-driven chat history management feature so users can view previous conversations when they reconnect.
FAQ
Why limit specific file types for uploads?
Limiting file types improves security by preventing potentially harmful files from being uploaded, while also aligning with common business document formats.
What is Multer used for in this context?
Multer is a Node.js middleware that simplifies handling multipart/form-data, which is required for file uploads. It allows you to specify storage locations, rename files, set size limits, and validate extensions.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 강좌] 13강 파일 업로드와 다운로드 기능 만들기