@abdurrahmanabid/multi-file-upload
Version:
"A simple middleware for handling single and multiple file uploads with customizable file types and directories."
113 lines (84 loc) ⢠3.37 kB
Markdown
# @abdurrahmanabid/multi-file-upload
A simple yet powerful Express.js middleware for handling **single and multiple** file uploads using Multer.
This package supports **custom file types, directory selection, error handling, and more**.
## š Features
ā
**Single & Multiple File Uploads**
ā
**Custom Upload Directories**
ā
**File Type Restrictions**
ā
**Multer-based Efficient Storage**
ā
**Comprehensive Error Handling**
## š Installation
Before using this package, ensure you have **Node.js** and **Express.js** installed.
### **1ļøā£ Install the Package**
Run the following command:
```bash
npm install @abdurrahmanabid/multi-file-upload
```
> **Note:** `express` must be installed as it's a peer dependency. If not installed, run:
```bash
npm install express
```
### **2ļøā£ Import the Package**
In your Express.js app:
```javascript
const uploadHandler = require("@abdurrahmanabid/multi-file-upload");
```
## šÆ **Usage Examples**
### **1ļøā£ Basic Single File Upload**
```javascript
const express = require("express");
const uploadHandler = require("@abdurrahmanabid/multi-file-upload");
const app = express();
app.post("/upload", uploadHandler("single"));
app.listen(3000, () => console.log("Server running on port 3000"));
```
ā
**Now, you can upload a single file using Postman!**
---
### **2ļøā£ Upload Multiple Files**
```javascript
app.post("/upload-multiple", uploadHandler("multiple"));
```
- Upload multiple files via **Postman** using `form-data` with key **file**.
### **3ļøā£ Restrict File Types (Only Images)**
```javascript
app.post("/upload-images", uploadHandler("single", ["image/png", "image/jpeg"]));
```
- This will **only** allow `.png` and `.jpg` files.
### **4ļøā£ Upload to a Custom Directory**
```javascript
app.post("/upload-avatar", uploadHandler("single", [], "uploads/avatars"));
```
- Files will be stored in **uploads/avatars/** instead of the default **uploads/**.
## š **Advanced Configuration**
| Parameter | Type | Description | Default |
|---------------|---------|--------------------------------------------------|-------------|
| `uploadType` | string | `"single"` or `"multiple"` | `"single"` |
| `acceptedTypes` | array | Allowed file types (e.g., `["image/png"]`) | `[]` (All) |
| `directory` | string | Directory where files will be stored | `"uploads/"` |
Example:
```javascript
app.post("/custom-upload", uploadHandler("multiple", ["image/png", "image/jpeg"], "custom_dir"));
```
## **š How to Use in Postman**
1. Open **Postman**.
2. Select **POST** method.
3. Enter the API endpoint, e.g., `http://localhost:3000/upload`.
4. Go to the **Body** tab ā Select **form-data**.
5. Use **Key**:
- `"file"` for **single** or **multiple** uploads.
6. Click **Select File**, upload a file.
7. Click **Send**.
## ā **Common Issues & Solutions**
| Issue | Cause | Solution |
|-------|------|-----------|
| `Cannot find module 'express'` | Express is not installed | Run `npm install express` |
| `MulterError: Unexpected field` | Incorrect field name in Postman | Use `file` for single & `files` for multiple |
| `Invalid file type` | Unsupported file format | Pass correct MIME types in `acceptedTypes` |