欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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)

flask文件的上传


相关标签: flask 文件上传