代码语言:javascript复制
from flask import Flask, render_template, request,Response,redirect,url_for
from werkzeug.utils import secure_filename
import os, numpy as np, random, shutil, json
app = Flask(__name__, template_folder='./html', static_url_path='')
@app.route('/', 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/uploads',secure_filename(f.filename))
#注意:没有的文件夹一定要先创建,不然会提示没有该路径 或者os.mkdir('')
f.save(upload_path)
return redirect(url_for('upload'))
# 或返回json对象
# return Response(json.dumps({'code': 0}), content_type='application/json')
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
文件结构: