1.0.1 ⢠Published 9 months ago
@abdurrahmanabid/multi-file-upload v1.0.1
@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:
npm install @abdurrahmanabid/multi-file-uploadNote:
expressmust be installed as it's a peer dependency. If not installed, run:npm install express
2ļøā£ Import the Package
In your Express.js app:
const uploadHandler = require("@abdurrahmanabid/multi-file-upload");šÆ Usage Examples
1ļøā£ Basic Single File Upload
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
app.post("/upload-multiple", uploadHandler("multiple"));- Upload multiple files via Postman using
form-datawith key file.
3ļøā£ Restrict File Types (Only Images)
app.post("/upload-images", uploadHandler("single", ["image/png", "image/jpeg"]));- This will only allow
.pngand.jpgfiles.
4ļøā£ Upload to a Custom Directory
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:
app.post("/custom-upload", uploadHandler("multiple", ["image/png", "image/jpeg"], "custom_dir"));š How to Use in Postman
- Open Postman.
- Select POST method.
- Enter the API endpoint, e.g.,
http://localhost:3000/upload. - Go to the Body tab ā Select form-data.
- Use Key:
"file"for single or multiple uploads.
- Click Select File, upload a file.
- 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 |