在使用flask定义路由完成文件上传时,定义upload视图函数
from flask import Flask, render_template
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
app.debug = True
app.secret_key = 'helloworld!!'
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/upload',methods=['GET','POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
base_path = os.path.abspath(os.path.dirname(__file__))
upload_path = os.path.join(base_path,'static\uploads')
f.save(upload_path,secure_filename(f.filename))
return "文件上传成功!!"
return render_template('upload.html')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'),404