flask文件的上传
Posted 岩枭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask文件的上传相关的知识,希望对你有一定的参考价值。
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)
以上是关于flask文件的上传的主要内容,如果未能解决你的问题,请参考以下文章