fix(admin): 简化附件下载,直接使用Django媒体代理
This commit is contained in:
parent
79706dd840
commit
54230b6cfd
|
|
@ -1,7 +1,4 @@
|
||||||
from rest_framework import generics, viewsets
|
from rest_framework import generics, viewsets
|
||||||
from rest_framework.response import Response
|
|
||||||
from rest_framework.decorators import action
|
|
||||||
from django.http import FileResponse
|
|
||||||
from .models import Application
|
from .models import Application
|
||||||
from .serializers import ApplicationCreateSerializer, ApplicationSerializer, ApplicationStatusSerializer
|
from .serializers import ApplicationCreateSerializer, ApplicationSerializer, ApplicationStatusSerializer
|
||||||
from .emails import notify_status_change
|
from .emails import notify_status_change
|
||||||
|
|
@ -31,25 +28,6 @@ class ApplicationManageViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
job__organization=user.organization
|
job__organization=user.organization
|
||||||
).select_related('job__organization', 'applicant')
|
).select_related('job__organization', 'applicant')
|
||||||
|
|
||||||
@action(detail=True, methods=['get'])
|
|
||||||
def download_resume(self, request, pk=None):
|
|
||||||
"""下载求职者的简历附件"""
|
|
||||||
application = self.get_object()
|
|
||||||
attachment_url = application.resume_snapshot.get('attachment_url')
|
|
||||||
|
|
||||||
if not attachment_url:
|
|
||||||
return Response({'detail': '附件不可用'}, status=404)
|
|
||||||
|
|
||||||
try:
|
|
||||||
from django.core.files.storage import default_storage
|
|
||||||
# 获取文件
|
|
||||||
file = default_storage.open(attachment_url.lstrip('/media/'), 'rb')
|
|
||||||
response = FileResponse(file)
|
|
||||||
response['Content-Type'] = 'application/octet-stream'
|
|
||||||
response['Content-Disposition'] = f'attachment; filename="{application.resume_snapshot.get("name", "resume")}.pdf"'
|
|
||||||
return response
|
|
||||||
except Exception as e:
|
|
||||||
return Response({'detail': f'下载失败: {str(e)}'}, status=400)
|
|
||||||
|
|
||||||
class ApplicationStatusUpdateView(generics.UpdateAPIView):
|
class ApplicationStatusUpdateView(generics.UpdateAPIView):
|
||||||
serializer_class = ApplicationStatusSerializer
|
serializer_class = ApplicationStatusSerializer
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,6 @@ const applications = ref([])
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const resumeVisible = ref(false)
|
const resumeVisible = ref(false)
|
||||||
const currentResume = ref(null)
|
const currentResume = ref(null)
|
||||||
const currentApplicationId = ref(null)
|
|
||||||
|
|
||||||
const genderMap = {
|
const genderMap = {
|
||||||
'male': '男',
|
'male': '男',
|
||||||
|
|
@ -129,7 +128,6 @@ async function updateStatus(row) {
|
||||||
|
|
||||||
function viewResume(row) {
|
function viewResume(row) {
|
||||||
currentResume.value = row.resume_snapshot
|
currentResume.value = row.resume_snapshot
|
||||||
currentApplicationId.value = row.id
|
|
||||||
resumeVisible.value = true
|
resumeVisible.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,29 +135,19 @@ function getGenderLabel(gender) {
|
||||||
return genderMap[gender] || '-'
|
return genderMap[gender] || '-'
|
||||||
}
|
}
|
||||||
|
|
||||||
async function downloadAttachment() {
|
function downloadAttachment() {
|
||||||
if (!currentApplicationId.value) {
|
if (!currentResume.value?.attachment_url) {
|
||||||
ElMessage.warning('无法获取投递记录')
|
ElMessage.warning('附件不可用')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
// 直接通过 media URL 下载(由 Django 代理)
|
||||||
const response = await fetch(`/api/applications/manage/${currentApplicationId.value}/download_resume/`)
|
|
||||||
if (!response.ok) throw new Error('下载失败')
|
|
||||||
|
|
||||||
const blob = await response.blob()
|
|
||||||
const url = window.URL.createObjectURL(blob)
|
|
||||||
const link = document.createElement('a')
|
const link = document.createElement('a')
|
||||||
link.href = url
|
link.href = currentResume.value.attachment_url
|
||||||
link.download = `${currentResume.value.name}_resume.pdf`
|
link.download = `${currentResume.value.name}_resume.pdf`
|
||||||
document.body.appendChild(link)
|
document.body.appendChild(link)
|
||||||
link.click()
|
link.click()
|
||||||
document.body.removeChild(link)
|
document.body.removeChild(link)
|
||||||
window.URL.revokeObjectURL(url)
|
|
||||||
ElMessage.success('下载已启动')
|
ElMessage.success('下载已启动')
|
||||||
} catch (e) {
|
|
||||||
ElMessage.error('下载失败,请重试')
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue