├── uploads ├── bird_1670424361568.jpg ├── tree_1670473495979.jpg └── 4-5 Verticle _1686291663555.jpg ├── package.json ├── views └── signup.ejs └── index.js /uploads/bird_1670424361568.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sukeshperiyasamy/Testimonial/HEAD/uploads/bird_1670424361568.jpg -------------------------------------------------------------------------------- /uploads/tree_1670473495979.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sukeshperiyasamy/Testimonial/HEAD/uploads/tree_1670473495979.jpg -------------------------------------------------------------------------------- /uploads/4-5 Verticle _1686291663555.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sukeshperiyasamy/Testimonial/HEAD/uploads/4-5 Verticle _1686291663555.jpg -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fileupload", 3 | "version": "1.0.0", 4 | "description": "fileupload using nodejs", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "JV Logesh", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ejs": "^3.1.9", 13 | "express": "^4.18.2", 14 | "multer": "^1.4.5-lts.1" 15 | }, 16 | "devDependencies": {} 17 | } 18 | -------------------------------------------------------------------------------- /views/signup.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Testimonials 8 | 9 | 10 |

Upload-Photo

11 |
12 | Upload-File 13 |
14 | 15 |
16 | 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const path = require('path'); 4 | const multer = require('multer'); 5 | const { log } = require('console'); 6 | 7 | app.set('views',path.join(__dirname, "views" )); 8 | app.set('view engine',"ejs"); 9 | 10 | var storage = multer.diskStorage({ 11 | destination : function(req, file, cb){ 12 | 13 | //some work 14 | cb(null, 'uploads') 15 | }, 16 | filename :function(req, file, cb){ 17 | cb(null, file.originalname.replace(/\.[^/.]+$/,"") + '_' + Date.now() + path.extname(file.originalname) ) 18 | } 19 | }) 20 | 21 | let maxSize = 2 * 1000 * 1000 22 | 23 | let upload = multer({ 24 | storage : storage, 25 | limits : { 26 | fileSize : maxSize 27 | }, 28 | fileFilter : function (req, file, cb){ 29 | console.log(file.mimetype); 30 | let filetypes = /jpeg|jpg|png/; 31 | let mimetype = filetypes.test(file.mimetype); 32 | let extname = filetypes.test(path.extname(file.originalname).toLowerCase()) 33 | 34 | if(mimetype && extname){ 35 | return cb(null, true); 36 | } 37 | 38 | cb("Error: File upload only supports the following filetypes: " +filetypes ) 39 | 40 | } 41 | }).single('mypic'); 42 | 43 | 44 | app.get('/',(req,res)=>{ 45 | res.render('signup') 46 | }) 47 | 48 | app.post('/upload',(req, res, next)=>{ 49 | upload(req, res, function(err){ 50 | if(err) { 51 | if(err instanceof multer.MulterError && err.code == "LIMIT_FILE_SIZE"){ 52 | return res.send("File size is maximum 2mb"); 53 | } 54 | 55 | res.send(err); 56 | }else{ 57 | res.send("Success. Image Uploaded!") 58 | } 59 | }) 60 | }) 61 | 62 | app.listen(8080, ()=>{ 63 | console.log(`Server is Running`) 64 | }) 65 | --------------------------------------------------------------------------------