1.0.1 • Published 9 months ago

@abdurrahmanabid/multi-file-upload v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

@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-upload

Note: express must 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-data with key file.

3ļøāƒ£ Restrict File Types (Only Images)

app.post("/upload-images", uploadHandler("single", ["image/png", "image/jpeg"]));
  • This will only allow .png and .jpg files.

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

ParameterTypeDescriptionDefault
uploadTypestring"single" or "multiple""single"
acceptedTypesarrayAllowed file types (e.g., ["image/png"])[] (All)
directorystringDirectory where files will be stored"uploads/"

Example:

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

IssueCauseSolution
Cannot find module 'express'Express is not installedRun npm install express
MulterError: Unexpected fieldIncorrect field name in PostmanUse file for single & files for multiple
Invalid file typeUnsupported file formatPass correct MIME types in acceptedTypes
1.0.1

9 months ago

1.0.0

10 months ago