Node.js File Upload Example : NodeJS multer Package

Other Blogs

Blogs ❯❯ NodeJS

Image could not load

NodeJS upload File

File upload का मतलब है की client से file को server पे transfer करना। ये images, documents, या किसी भी तरह की files हो सकती हैं. जब आप कोई file किसी website या app पे upload करते हैं, वो server पे save हो जाती है ताकि बाद में उससे access किया जा सके।

NodeJS Upload File Example

Step 1 : Setup Node.js Project

पहले, एक new directory बनाएं और Node.js project को initialize करें:

mkdir file-upload-tutorial
cd file-upload-tutorial
npm init -y

इस command से एक package.json file create होगी जो आपके project dependencies को manage करेगी।

Step 2 : Install Required Packages

अब आपको Express और Multer install करना है -

npm install express multer

Step 3 : Create Server File

एक server.js file बनाएं जो आपका main server file होगा. इस file में Express और Multer को configure करना होगा -

const express = require('express'); const multer = require('multer'); const path = require('path'); // Express app initialize karein const app = express(); // Storage configuration for Multer const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, 'uploads/'); }, filename: (req, file, cb) => { cb(null, Date.now() + path.extname(file.originalname)); } }); // Multer middleware setup const upload = multer({ storage: storage }); // Serve static files from 'public' directory app.use(express.static('public')); // Single file upload route app.post('/upload', upload.single('file'), (req, res) => { try { res.send('File uploaded successfully!'); } catch (error) { console.error(error); res.send('Failed to upload file.'); } }); // Server ko listen karayein const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });

Step 4 : Create Upload Form

अब आपको एक simple HTML form banana होगा जो file upload करेगा. इस form को public/index.html file में save करें

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Node.js File Upload</title> </head> <body> <h1>Upload File</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" required> <button type="submit">Upload</button> </form> </body> </html>

Run  Server

node server.js

NodeJS Security Considerations

1. File Type Validation : ये ensure करें की सिर्फ allowed file types upload हो सकती हैं , आप fileFilter option का use कर सकते हैं -

const upload = multer({ storage: storage, fileFilter: (req, file, cb) => { const filetypes = /jpeg|jpg|png|gif/; const mimetype = filetypes.test(file.mimetype); const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); if (mimetype && extname) { return cb(null, true); } cb('Error: File upload only supports the following filetypes - ' + filetypes); } });

2. File Size Limit : आप file size limit set कर सकते हैं ताकि large files upload न हो -

const upload = multer({ storage: storage, limits: { fileSize: 1000000 } // 1MB limit });

Conclusion

Node.js में file upload करना एक common task है जो बहुत तरीके से achieve किया जा सकता है। Multer middleware use करके आप easily और efficiently file uploads handle कर सकते हैं।

आप इस code को further customize कर सकते हैं अपनी specific requirements के according, जैसे की database integration, cloud storage, etc.

उम्मीद है की ये guide आपको Node.js में file uploads implement करने में मदद करेगी।

Happy coding!

Related Topic :

Recent Blogs

Loading ...

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    0 Comment(s) found !