const express = require('express')
const fileUpload = require('express-fileupload')
var router = express.Router()
var settings = require('../../settings')



router.use(fileUpload())

router.get('/', async function(req, res){
    settings.dLogo = 'ទំព័រ​ Upload'
    settings.message = ''

    if(req.session.user){
        res.render('users/upload', settings)
    }else{
        res.redirect('/users')
    }
})

router.post('/', async function(req, res) {
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.')
  }

  const uploadedFile = req.files.uploadFile
  const fileName = req.files.uploadFile.name
  const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2)
  const moveTo = `public/upload/${id+'_'+fileName}`
  const fileUrl = `/upload/${id+'_'+fileName}`

  uploadedFile.mv(moveTo, function(err) {
    if (err)
      return console.log(err)

    settings.dLogo = 'ទំព័រ​ Upload'
    settings.message = fileUrl

    res.render('users/upload', settings)
  })
})



module.exports = router

 

<%- include('partials/header.ejs') %>
<link href="/styles/users/category.css" rel="stylesheet"></link>
  
<section class='Main region'>
    <div class='sidebar'>
        <%- include('partials/menu.ejs') %>
    </div>

    <style>
        .Main .content form input{
            padding: 3px 0;
        }
        .Main .content form input[type=text]{
            text-align: center;
            font: var(--body-fornt);
        }
    </style>

    <div class='content'>
        <form action='/users/upload' method='post' enctype="multipart/form-data" >
            <a></a><input type='file' name='uploadFile' required />
            <a></a><input type='text' value='<%= message %>' placeholder='តំណរភ្ជាប់ឯ​កសារ​នៅ​លើ Server' />
            <a></a><input type='submit' value='បញ្ជូន​ទិន្នន័យ' />
        </form>
    </div>
</section>


<%- include('partials/footer.ejs') %>

 

GitHub: https://github.com/Sokhavuth/tv

Vercel: https://tv-neon.vercel.app

Responsive: http://www.responsinator.com