Commit ebd02fbc by 周田

feat: 文件的上传和下载

parent 4681552d
<template> <template>
<el-upload <el-upload
class="upload-demo" class="upload-demo"
action="test/upload/" action="api/protocol_version_manage/file_upload/"
:show-file-list="false" :show-file-list="false"
:data="data" :data="data"
> >
...@@ -23,7 +23,7 @@ const data = ref({ ...@@ -23,7 +23,7 @@ const data = ref({
}); });
const downloadFile = () => { const downloadFile = () => {
let url = "test/download/?protocol_name=HY_VirtualDevice_PROTOCOL&version=222"; let url = "api/protocol_version_manage/file_download/HY_VirtualDevice_PROTOCOL/222/";
axios({ axios({
url, url,
method: "GET", method: "GET",
......
/*
Navicat Premium Data Transfer
Source Server : test
Source Server Type : MySQL
Source Server Version : 80030
Source Host : 192.168.3.11:3306
Source Schema : mysql_oam_110
Target Server Type : MySQL
Target Server Version : 80030
File Encoding : 65001
Date: 22/12/2022 16:36:32
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for alarm_attribution
-- ----------------------------
DROP TABLE IF EXISTS `alarm_attribution`;
CREATE TABLE `alarm_attribution` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '序号,自增',
`alarmid` bigint(0) NULL DEFAULT NULL COMMENT '告警 ID',
`alarmlevel` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '告警级别',
`alarmdescriptionchn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '中文描述,应该描述故障现象 和解决问题的方法',
`alarmdescriptioneng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '英文描述',
`ismon` int(0) NULL DEFAULT NULL COMMENT '是否监视',
`updatetime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最新更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 176 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for current_alarm
-- ----------------------------
DROP TABLE IF EXISTS `current_alarm`;
CREATE TABLE `current_alarm` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '告警序号',
`alarmid` bigint(0) NULL DEFAULT NULL COMMENT '告警 ID,取自告警配置列表中的 ID',
`stationid` int(0) NULL DEFAULT NULL COMMENT '告警源头,SUID 标识',
`deviceid` int(0) NULL DEFAULT NULL COMMENT '告警子设备',
`alarmlevel` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmdescriptionchn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '告警描述',
`alarmdescriptioneng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`reporttime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '告警产生时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for current_performance
-- ----------------------------
DROP TABLE IF EXISTS `current_performance`;
CREATE TABLE `current_performance` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`perid` int(0) NULL DEFAULT NULL,
`stationid` int(0) NULL DEFAULT NULL,
`deviceid` int(0) NULL DEFAULT NULL,
`datacatlog` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fieldname` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fieldvalue` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`perdescriptionchn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`perdescriptioneng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`updatetime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for history_alarm
-- ----------------------------
DROP TABLE IF EXISTS `history_alarm`;
CREATE TABLE `history_alarm` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`alarmid` int(0) NULL DEFAULT NULL COMMENT '告警 ID,取自告警配置列表中的 ID',
`stationid` int(0) NULL DEFAULT NULL COMMENT '告警源头,SUID标识',
`deviceid` int(0) NULL DEFAULT NULL,
`alarmlevel` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmdescriptionchn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmdescriptioneng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`starttime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '告警产生时间 ',
`endtime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '告警结束时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for log_info
-- ----------------------------
DROP TABLE IF EXISTS `log_info`;
CREATE TABLE `log_info` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`stationid` int(0) NULL DEFAULT NULL,
`deviceid` int(0) NULL DEFAULT NULL,
`manipunator` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`loglevel` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`classification` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`loginfochn` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`loginfoeng` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`reporttime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for per_attribution
-- ----------------------------
DROP TABLE IF EXISTS `per_attribution`;
CREATE TABLE `per_attribution` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`perid` bigint(0) NULL DEFAULT NULL,
`datacatlog` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fieldname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`perdescriptionchn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`perdescriptioneng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`ismon` int(0) NULL DEFAULT NULL,
`isfixed` int(0) NULL DEFAULT NULL,
`fieldunit` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fieldtype` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`operator1` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`operand1` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`relation` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`operator2` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`operand2` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmlevel` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmdeschn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`alarmdeseng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`updatetime` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3435 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- auto-generated definition
create table history_performance
(
id int auto_increment
primary key,
suid int not null comment '1/1/1/9 1/1/2/6 ',
value varchar(100) not null comment 'EbN0,BER,中频输入电压,ACU 20.5,1.2-34,-12.5,255',
time varchar(30) not null comment '2023-06-05 06:05:25'
);
SET FOREIGN_KEY_CHECKS = 1;
...@@ -8,6 +8,8 @@ urlpatterns = [ ...@@ -8,6 +8,8 @@ urlpatterns = [
re_path(r'^protocol_version_manage/change_protocol_version/$', views.change_protocol_version), re_path(r'^protocol_version_manage/change_protocol_version/$', views.change_protocol_version),
re_path(r'^protocol_version_manage/add_protocol_version/$', views.add_protocol_version), re_path(r'^protocol_version_manage/add_protocol_version/$', views.add_protocol_version),
re_path(r'^protocol_version_manage/update_protocol_version/$', views.update_protocol_version), re_path(r'^protocol_version_manage/update_protocol_version/$', views.update_protocol_version),
# re_path(r'^protocol_version_manage/file_upload/$', views.file_upload), re_path(r'^protocol_version_manage/file_upload/$', views.raw_file_upload),
re_path(r'^protocol_version_manage/file_download/(?P<protocol_name>.+)/(?P<version>.+)/$',
views.raw_file_download),
re_path(r'^all_protocol_version/$', views.AllProtocolVersionViewSet.as_view({'get': 'list'})), re_path(r'^all_protocol_version/$', views.AllProtocolVersionViewSet.as_view({'get': 'list'})),
] ]
import os
import json import json
import urllib.parse
from django.conf import settings
from django.http import FileResponse
from rest_framework.decorators import api_view, parser_classes from rest_framework.decorators import api_view, parser_classes
from rest_framework.parsers import MultiPartParser from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response from rest_framework.response import Response
...@@ -79,5 +83,56 @@ class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin): ...@@ -79,5 +83,56 @@ class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin):
@api_view(['POST']) @api_view(['POST'])
@parser_classes([MultiPartParser]) @parser_classes([MultiPartParser])
def file_upload(request): def raw_file_upload(request):
pass # 将上传来的文件保存下来
file_obj = request.FILES.get('file')
protocol_name = request.data.get('protocol_name')
version = request.data.get('version')
assert protocol_name is not None and version is not None, Response(status=status.HTTP_400_BAD_REQUEST)
try:
folder_path = os.path.join(settings.BASE_DIR, 'protocol_raw_files', protocol_name, version)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
file_path = os.path.join(folder_path, file_obj.name)
with open(file_path, 'wb') as f:
for chunk in file_obj.chunks():
f.write(chunk)
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# 更新协议版本信息,将文件路径保存下来
protocol_versions = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first()
version_path_list = json.loads(protocol_versions.version_paths)
for version_path in version_path_list:
if version_path['version'] == version:
if version_path.get('path', None) is not None:
os.remove(version_path['path'])
version_path['path'] = file_path
break
protocol_versions.version_paths = json.dumps(version_path_list)
protocol_versions.save()
return Response(data={'path': file_path}, status=status.HTTP_200_OK)
@api_view(['GET'])
@parser_classes([MultiPartParser])
def raw_file_download(request, protocol_name, version):
try:
protocol_versions = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first()
for version_path in json.loads(protocol_versions.version_paths):
if version_path['version'] == version:
file_path = version_path['path']
break
file_name = urllib.parse.quote(os.path.basename(file_path))
response = FileResponse(open(file_path, 'rb'))
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
response['Content-Disposition'] = 'attachment; filename=' + file_name
response['Content-Type'] = 'multipart/form-data'
response['filename'] = file_name
return response
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment