Guest User

Untitled

a guest
Sep 10th, 2020 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. const express = require('express')
  2. const fileUpload = require('express-fileupload');
  3. const app = express()
  4. const port = 3000
  5.  
  6. app.use(fileUpload());
  7.  
  8. app.post('/upload', (req, res) => {
  9. console.log(req.headers);
  10.  
  11. if (!req.files || Object.keys(req.files).length === 0) {
  12. return res.status(400).send('No files were uploaded.');
  13. }
  14.  
  15. // The name of the input field (i.e. "file1") is used to retrieve the uploaded file (as defined in the activity attachments)
  16. let sampleFile = req.files.file1;
  17.  
  18. // Use the mv() method to place the file somewhere on your server
  19. let fName = Date.now(); // get the current timestamp
  20. sampleFile.mv('uploads/'+fName+'.jpg', function(err) {
  21. if (err)
  22. return res.status(500).send(err);
  23.  
  24. res.send('File uploaded!');
  25. console.log('File saved to '+fName+'.jpg');
  26. });
  27. })
  28.  
  29. app.listen(port, () => {
  30. console.log(`Example app listening at http://localhost:${port}`)
  31. })
  32.  
Add Comment
Please, Sign In to add comment