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)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章