flask文件的上传
程序员文章站
2022-06-03 08:52:25
...
flask文件的上传:
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flask服务器</title>
</head>
<body>
<h1>文件上传</h1>
<form action="" enctype="multipart/form-data" method='post'>
<input type="file" name="file">
<input type="submit" name="上传">
</form>
</body>
</html>
test.py
from flask import Flask,render_template,request,redirect,url_for,send_from_directory
from werkzeug.utils import secure_filename
import os
app=Flask(__name__)
@app.route('/upload',methods=['POST','GET'])
def upload():
if request.method=='POST':
f=request.files['file']
basepath=os.path.dirname(__file__)#当前文件所在路径
upload_path=os.path.join(basepath,'static/upload',secure_filename(f.filename))
f.save(upload_path)
return redirect(url_for('upload'))
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True,port=6699)
上一篇: ajax异步上传文件
下一篇: PHP面向对象——多态