index.heml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
上传图片
<form action="/uploads/" method="post" enctype="multipart/form-data">
<input type="file" name="pic">
<input type="submit" value="上传"></input>
</form>
</body>
</html>
run.py
import os
import time
from flask import Flask,render_template,request
app=Flask(__name__,template_folder='templates')
@app.route('/')
def index():
return render_template('/index.html')
@app.route('/uploads/',methods=['POST'])
def uploads():
file = request.files.get('pic')# 获取文件名称
if not file: # 如果为空还返回之前页面
return render_template('index.html')
file_name = time.strftime('%Y-%m-%d-%H-%M-%S') + file.filename
file_url = f'/static/{file_name}'
file.save(os.path.join(
app.root_path,
app.static_folder,
file_name
))
return file_url
if __name__ == '__main__':
app.run(debug=True)
标签:__,文件,name,-%,app,file,上传,template
From: https://www.cnblogs.com/fengkunlei/p/18621984