文件上传接口
import os
import pandas as pd
import json
from flask import request, Flask
app = Flask(__name__)
upload_export_path = 'D:\\python\\bushu1\\jiekou\\file\\'
moban_path = 'D:\\python\\bushu1\\jiekou\\moban\\'
@app.route('/upload', methods=['POST'])
def upload_file():
"""
:return:
"""
file_buffer = request.files['file']
f_name = file_buffer.filename
if os.path.exists(upload_export_path + f_name):
return json.dumps({"code": 201, 'message': '文件已存在,请重新上传!'}, ensure_ascii=False)
elif not os.path.exists(upload_export_path + f_name):
file_buffer.save(upload_export_path + f_name)
data = pd.read_excel(moban_path + '测试用例模板.xlsx', header=None)
df = pd.concat([data[data.columns[0]], data[data.columns[:12]]], axis=1)
t = df.values.tolist()
list1 = t[0]
data = pd.read_excel(upload_export_path + f_name, header=None, sheet_name=None)
for j in data.values():
df = pd.concat([j[j.columns[0]], j[j.columns[:12]]], axis=1)
j1 = df.values.tolist()
list2 = j1[0]
if list1 != list2:
os.remove(upload_export_path + f_name)
return json.dumps({"code": 202, 'message': '上传的文件标题与模板不符'}, ensure_ascii=False)
return json.dumps({"code": 200, 'message': '上传成功!'}, ensure_ascii=False)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)