首页 > 代码库 > Flask入门-上传的问题
Flask入门-上传的问题
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> <h1>文件上传实例</h1> <form action="" method="post" enctype="multipart/form-data"> <p> <input type="file" name="file"> <input type="submit" value="http://www.mamicode.com/Upload"> </p> </form> </body> </html>
本文出自 “joker” 博客,请务必保留此出处http://jokerbug.blog.51cto.com/12943231/1947849
Flask入门-上传的问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。