Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const fileUpload = require('express-fileupload');
- const app = express()
- const port = 3000
- app.use(fileUpload());
- app.post('/upload', (req, res) => {
- console.log(req.headers);
- if (!req.files || Object.keys(req.files).length === 0) {
- return res.status(400).send('No files were uploaded.');
- }
- // The name of the input field (i.e. "file1") is used to retrieve the uploaded file (as defined in the activity attachments)
- let sampleFile = req.files.file1;
- // Use the mv() method to place the file somewhere on your server
- let fName = Date.now(); // get the current timestamp
- sampleFile.mv('uploads/'+fName+'.jpg', function(err) {
- if (err)
- return res.status(500).send(err);
- res.send('File uploaded!');
- console.log('File saved to '+fName+'.jpg');
- });
- })
- app.listen(port, () => {
- console.log(`Example app listening at http://localhost:${port}`)
- })
Add Comment
Please, Sign In to add comment