html

1
2
3
4
<form method="post" enctype="multipart/form-data">
<input type="file" name="appendix" class="layui-input"/>
<button id="submit">提交</button>
</form>

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@bp.route('/upload', methods=['POST'])
@login_required
def upload():
uploaded_file = request.files.get('file')

suffix = '.' + uploaded_file.filename.split('.')[-1] # 获取文件后缀名
filename = uploaded_file.filename.replace(".", "").replace("\\", "").replace("/", "")
basedir = os.path.abspath(os.path.dirname(__file__)).replace("blueprints", "") # 获取当前文件路径
new_dir = os.path.join("static", "big_data_upload", filename + str(int(time.time())) + suffix) # 拼接相对路径
uploaded_file_path = os.path.join(basedir, new_dir) # 拼接图片完整保存路径,时间戳命名文件防止重复
uploaded_file.save(uploaded_file_path) # 保存文件
size = os.path.getsize(uploaded_file_path)
print(size)

return "success"