Adding Uploading File Functionality

#controllers/dashboard/upload.py
import os, config, copy, uuid
from flask import render_template, request, session
from werkzeug.utils import secure_filename

class Upload():
  def __init__(self):
    pass

  def get_post(self):
    vdict = copy.deepcopy(config.vdict)

    if (request.method == "POST") and ('logged-in' in session):
      f = request.files['fupload']
      if f != '':
        id = str(uuid.uuid4().int)
        ROOT_DIR = os.path.dirname(os.path.abspath("config.py"))
        savePath = ROOT_DIR + '/static/uploads/' + id + '_' + secure_filename(f.filename)
        url = '/static/uploads/' + id + '_' + secure_filename(f.filename)
        f.save(savePath)
        vdict['url'] = url
        return render_template('dashboard/uploadurl.html', data=vdict)
      else:
        return render_template('dashboard/upload.html', data=vdict)
      
    else:
      return render_template('dashboard/upload.html', data=vdict)
<!--templates/dashboard/upload.html-->
<!DOCTYPE html>
<html>
  <head>
    {% block head %}
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="This blog engine is built for e-learning">
    <meta name="robots" content="index, follow">
    <meta name='keywords' content='e-learning, learning, school, math, programming'>
    <title>{{data['blog_title']}}</title>
    <link href="/static/styles/base.css" rel="stylesheet">
    <link href="/static/styles/upload.css" rel="stylesheet">
    <link href="/static/fonts/setup.css" rel='stylesheet'>
    <link href="/static/images/site_logo.png" rel="icon">
    {% endblock %}
  </head>
  <body>
    <div id="main" class="main region">
      <div id="form-title">ទំរង់បែបបទ​ចំលង​ឯកសារ</div>
        {% block content %}
        <form id='content' action="/dashboard/upload/" method="POST" enctype="multipart/form-data">
          <a>ជ្រើសរើស​ឯកសារៈ</a><input type="file" name="fupload" required />
          <a></a><input type='submit' value="ចំលងទុក​ក្នុង​គេហទំព័រ" />
        </form>
        {% endblock %}
    </div>

    <div id="footer" class="footer region">
      {% block footer %}
      &copy; Copyright 2020 by <a href="https://www.khmerweb.app/">Khmer Web</a>.
      {% endblock %}
    </div>
  </body>
</html>
/*static/styles/upload.css*/
body{
  background: grey;
}
#main{
  width: 450px;
  margin: 30px auto;
  text-align: center;
  background: white;
}
#main #form-title{
  padding: 8px;
  font: 16px/1.5 Moul;
  background: rgb(219, 58, 0);
  color: white;
}
#content{
  display: grid;
  grid-template-columns: 110px calc(100% - 110px);
  grid-gap: 7px 5px;
  align-items: center;
  padding: 20px;
}
#content a{
  text-align: right;
}
#content input{
  font: 14px/1.5 OdorMeanChey;
  padding: 4px;
}

GitHub: "https://github.com/Sokhavuth/E-Learning
Heroku: https://khmerweb-elearning.herokuapp.com/

Comments

Popular posts from this blog

Boilerplate Code

Adding Frontend Single Movie Template