Commit 20300e14 by 周田

feat:sqlite文件导出

parent 1f2981b5
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class DownloadDbConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'download_db'
from django.urls import path
from . import views
urlpatterns = [
path('downloadFile/', views.index),
]
from django.conf import settings
from django.http import FileResponse, HttpResponse
from rest_framework import status
import os
def index(request):
file_path = os.path.join(settings.BASE_DIR, 'xdc.sqlite')
print(f"file path => {file_path}")
if os.path.exists(file_path):
response = FileResponse(open(file_path, 'rb'))
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
response['Content-Type'] = 'application/x-sqlite3'
return response
return HttpResponse(status=status.HTTP_404_NOT_FOUND)
<script setup lang="ts">
import Chat from '@/views/Chat'
// import Chat from '@/views/Chat'
import FileDown from './views/FileDown.vue';
</script>
<template>
<chat />
<!-- <chat /> -->
<FileDown />
</template>
<script setup lang="ts">
import { ElButton } from 'element-plus';
import axiox from 'axios';
const downloadFile = () => {
let url = '/api/downloadFile/'
axiox({
url,
method: 'GET',
responseType: 'blob'
}).then(res => {
// 下载文件
console.log(res)
const blob = new Blob([res.data], { type: 'application/x-sqlite3' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'xdc.sqlite';
link.click();
})
console.log('downloadFile');
}
</script>
<template>
<ElButton type="primary" @click="downloadFile">导出文件</ElButton>
</template>
\ No newline at end of file
......@@ -39,6 +39,7 @@ urlpatterns = [
path("mqtt/", include("mqtt.urls")),
path("chat/", include("chat.urls")),
path("op/", include("device_data_op.urls")),
path("api/", include("download_db.urls")),
re_path(r'^assets/(?P<path>.*)/$', serve, {'document_root': settings.STATIC_ROOT}),
......
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