flask.py
from flask import Flask, render_template, request, redirect, url_for from werkzeug.utils import secure_filename from os import path app = Flask(__name__) @app.route('/') def hello_world(): return render_template('index.html', title='Welcome') @app.route('/services') def services(): return 'Servisce' @app.route('/about') def about(): return 'About' @app.route('/user/<int:user_id>') def user(user_id): return 'User %d' % user_id @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] else: username = request.args['username'] return render_template('login.html', method=request.method) @app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': file = request.files['file'] basepath = path.abspath(path.dirname(__file__)) upload_path = path.join(basepath, 'static/uploads') filename = secure_filename(file.filename) file.save(path.join(upload_path,filename)) return redirect(url_for('upload')) return render_template('upload.html') if __name__ == '__main__': app.run(debug=True)
upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h2>文件上传实例</h2> <form action="" method="post" enctype="multipart/form-data"> <p> <input type="file" name="file"> <input type="submit" value="Upload"> </p> </form> </body> </html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。