左侧导航栏样式做到了统一
This commit is contained in:
@@ -171,13 +171,26 @@ def delete_dataset(id):
|
||||
|
||||
# ============ 数据集文件上传接口 ============
|
||||
|
||||
@datasets_bp.route('/upload/<int:dataset_id>', methods=['POST'])
|
||||
def make_response_with_cors(data, status=200):
|
||||
"""创建带CORS头的响应"""
|
||||
response = jsonify(data)
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
|
||||
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
|
||||
return response
|
||||
|
||||
@datasets_bp.route('/upload/<int:dataset_id>', methods=['POST', 'OPTIONS'])
|
||||
def upload_dataset_file(dataset_id):
|
||||
"""上传数据集文件"""
|
||||
# 处理 OPTIONS 预检请求
|
||||
if request.method == 'OPTIONS':
|
||||
response = make_response_with_cors({'status': 'ok'})
|
||||
return response
|
||||
|
||||
# 检查数据集是否存在
|
||||
dataset = generic_get_by_id('dataset_manage', dataset_id)
|
||||
if not dataset:
|
||||
return jsonify({'code': 1, 'message': '数据集不存在'})
|
||||
return make_response_with_cors({'code': 1, 'message': '数据集不存在'})
|
||||
|
||||
# 确保上传目录存在(datasets根目录)
|
||||
os.makedirs(DATASET_FOLDER, exist_ok=True)
|
||||
@@ -186,7 +199,7 @@ def upload_dataset_file(dataset_id):
|
||||
errors = []
|
||||
|
||||
if 'files' not in request.files:
|
||||
return jsonify({'code': 1, 'message': '没有文件被上传'})
|
||||
return make_response_with_cors({'code': 1, 'message': '没有文件被上传'})
|
||||
|
||||
files = request.files.getlist('files')
|
||||
|
||||
@@ -205,8 +218,9 @@ def upload_dataset_file(dataset_id):
|
||||
file.save(file_path)
|
||||
file_size = os.path.getsize(file_path)
|
||||
|
||||
# 获取文件扩展名
|
||||
ext = filename.rsplit('.', 1)[1].lower()
|
||||
# 获取文件扩展名(安全处理无扩展名的情况)
|
||||
parts = filename.rsplit('.', 1)
|
||||
ext = parts[1].lower() if len(parts) > 1 else 'unknown'
|
||||
|
||||
# 保存文件信息到数据库
|
||||
conn = get_db_connection()
|
||||
@@ -245,7 +259,7 @@ def upload_dataset_file(dataset_id):
|
||||
conn.close()
|
||||
|
||||
if errors:
|
||||
return jsonify({
|
||||
return make_response_with_cors({
|
||||
'code': 0,
|
||||
'message': f'部分文件上传成功,{len(errors)}个文件失败',
|
||||
'data': {
|
||||
@@ -254,7 +268,7 @@ def upload_dataset_file(dataset_id):
|
||||
}
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
return make_response_with_cors({
|
||||
'code': 0,
|
||||
'message': f'成功上传 {len(uploaded_files)} 个文件',
|
||||
'data': {
|
||||
|
||||
Reference in New Issue
Block a user