from werkzeug.utils import secure_filename
APP_CONFIG_UPLOAD_PATH = "UPLOAD_PATH"
# app.config可以用来保存一些常量
app.config[APP_CONFIG_UPLOAD_PATH] = os.path.join(app.root_path, "upload")
# 限制文件尺寸为16M
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
Upload
@app.route("/upload", methods=["GET", "POST"])
def upload():
if not os.path.exists(app.config[APP_CONFIG_UPLOAD_PATH]):
os.makedirs(app.config[APP_CONFIG_UPLOAD_PATH])
if flask.request.method == "POST":
fd = flask.request.files["file"]
# secure_filename(fd.filename): 获取文件名
fd.save(os.path.join(app.config[APP_CONFIG_UPLOAD_PATH], secure_filename(fd.filename)))
return ""
return "success"
Download
@app.route("/download", methods=["GET"])
def download():
if not os.path.exists(app.config[APP_CONFIG_UPLOAD_PATH]):
os.makedirs(app.config["UPLOAD"])
return flask.send_from_directory("upload", "AdobePhotoshop19-mul_x64.zip", as_attachment=True)