index.html
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../static/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="../static/js/bootstrap.js"></script>
</head>
<body>
<div class="rowcol-sm-3 col-sm-offset-4">
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="file" accept="image/*" multiple>
</div>
<div>
<input type="submit" class="btn btn-primary btn-lg" value="upload">
</div>
</form>
</div>
</body>
</html>
โค้ด: เลือกทั้งหมด
import os
from flask import Flask
from flask import render_template
from flask import request
โค้ด: เลือกทั้งหมด
app_path = os.path.dirname(os.path.abspath(__file__))
โค้ด: เลือกทั้งหมด
@app.route ( '/' )
def index():
return render_template ( 'index.html' )[/cod ที่เราจะเขียนฟังก์ชั่นในการ
สร้างหน้าฟังก์ชั่น upload
[code]@app.route("/upload", methods=['POST' , 'GET'])
def upload():
target = os.path.join ( app_path, "images/" )
print ( target )
if not os.path.isdir ( target ):
os.mkdir ( target )
for file in request.files.getlist ( "file" ):
print ( file )
filename = file.filename
destination = "/".join ( [target, filename] )
print ( destination )
file.save ( destination )
return "Upload Success"
โค้ด: เลือกทั้งหมด
target = os.path.join ( app_path, "images/" )
ถ้าภายในโปรเจคไม่มี folder images ให้ทำการสร้าง folder ใหม่
โค้ด: เลือกทั้งหมด
if not os.path.isdir ( target ):
os.mkdir ( target )
โค้ด: เลือกทั้งหมด
for file in request.files.getlist ( "file" ):
print ( file )
filename = file.filename
โค้ด: เลือกทั้งหมด
destination = "/".join ( [target, filename] )
print ( destination )
file.save ( destination )
อ้างอิง
https://docs.python.org/3/library/os.html
https://getbootstrap.com/docs/3.3/css/
https://www.palletsprojects.com/p/flask/