文件上传:
from flask import Flask, request, render_template # secure_filename:用户传递进来的文件名可以进行改造,是不可信赖的,使用secure_filename可以进行安全过滤 from werkzeug.utils import secure_filename app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': # 获取表单传递过来的参数 f = request.files['pics'] # 进行存储 f.save('static/image/'+secure_filename(f.filename)) return "file successfully..." # 渲染模板 return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>index</title>
</head>
<body>
<!-- 在上传文件时multipart/form-data是必不可少的 -->
<form action="" method="POST" enctype="multipart/form-data">
files loading:<input type="file" name="pics">
<input type="submit" value="sub">
</form>
</body>
</html>