router.post('/uploadimg/',upload.single('imgfile'),function(req,res,next){ let file = req.file; console.log(file); res.json({ status:1, code: 200, data:{ originname:file.originalname, generatename:file.filename } }) })
router.post("/image", (req, res) => { // You can save the image you have get. upload(req, res, (err) => { if (err) { return res.json({ success: false, err }); } return res.json({ success: true, filePath: res.req.file.path, fileName: res.req.file.filename, }); }); });
fse.unlink( path.join(req.file.destination, req.file.filename) ) .then(() => next()) .catch(next)
router.post('/', async(req,res) => { const {title, author, isbn} = req.body; const imagePath = '/uploads/' + req.file.filename; const newBook = new Book({title, author, isbn, imagePath}) await newBook.save(); res.json({message: 'Book Saved'}); })
router.post('/', checkPermission, upload.single('file'), (req, res, next) => { const { file } = req; const newFile = { description: req.body.description ? req.body.description : 'No description', filename: file.originalname, path: '/upload/' + file.filename, id: file.id }; File.add(newFile, (status, message) => { res.json({ status, message, file: newFile }); }); });
// Set the profile picture of a user router.post("/profile/img", middleware.isLogedIn, upload.single("file"), (req, res)=>{ if(req.file){ const file = { path: "/files/image/" + req.file.filename, }; User.findByIdAndUpdate(req.user._id, { profile_picture: "/files/image/" + req.file.filename }).then(()=>{ res.send(file); }); }else{ res.json({ error: true }); } });
app.post('/signup', upload.single('avatar'), async (req, res) => { console.log('req.body', req.body) console.log('req.file', req.file) const oldPath = __dirname + '/uploads/' + req.file.filename const newPath = __dirname + '/images/' + 'avatar_' + req.body.username + '.' + req.file.mimetype.split('/').pop() await fs.rename(oldPath, newPath) const user = await Users.create({ username: req.body.username, password: req.body.password, // NOTE: in production we save hash of password email: req.body.email, avatar: '/images/' + 'avatar_' + req.body.username + '.' + req.file.mimetype.split('/').pop() }) res.status(201).send(`User ${user.id} created`) })
router.post('/room/:id/gif', upload.single('gif'), async (req, res, next) => { try { const chat = await Chat.create({ room: req.params.id, user: req.session.color, gif: req.file.filename, }); req.app.get('io').of('/chat').to(req.params.id).emit('chat', chat); res.send('ok'); } catch (error) { console.error(error); next(error); } });
app.post('/api/avatar', upload.single('avatar'), (req, res) => { users.updateAvatar(req.body.id, req.file.filename); res.send(req.file); });
router.post(Constants.API_PREFIX + '/upload', upload.single('file'), (req, res) => { return res.send(serverUrl + '/public/uploads/' + req.file.filename) })
app.post('/upload', upload.single('myFile'), (req, res) => { if (req.file) { console.log('Uploading', req.file); var filename = req.file.filename; var uploadStatus = 'File Uploaded Successfully'; } else { console.log('No File Uploaded'); var filename = 'FILE NOT UPLOADED'; var uploadStatus = 'File Upload Failed'; } res.render('index.hbs', { status: uploadStatus, filename: `Name Of File: ${filename}` }); });
readChunk(path.join(req.file.destination, req.file.filename), 0, 8) .then((buffer) => { req.file.buffer = buffer; return next(); }) .catch(next)
router.post('/', upload.single('insurance'), (req, res) => { if (req.file !== undefined) { const newOffer = new Insurance({ files_id: req.file.id, image: '/api/insurances/image/' + req.file.filename, name: req.body.name, // Grab the file id that was stored in the database by the storage engine as the reference to your file }) newOffer.save().then(insurance => res.json(insurance)); } });
app.post('/api/upload', upload.single('image'), (req, res) => { if (req.file) res.json({ imageUrl: 'images/'+req.file.filename }); else res.status("409").json("No Files to Upload."); });
// Set the profile picture of a user router.post("/profile/img", middleware.isLogedIn, upload.single("file"), (req, res)=>{ if(req.file){ const file = { path: "/files/image/" + req.file.filename, }; User.findByIdAndUpdate(req.user._id, { profile_picture: "/files/image/" + req.file.filename }).then(()=>{ res.send(file); }); }else{ res.json({ error: true }); } });