Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:downloadfile #112

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/api/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ export const fileDownload = (url: string) => {
responseType: 'blob',
})
}
/**
* 下载验证
* @param url 需要下载的文件url
* @returns axios对象
*/

export const downloadCertificate = (url: string) => {
return apis({
method: 'get',
url: '/com/file/downloadCertificate',
params: {
url: url,
},
})
}
80 changes: 32 additions & 48 deletions src/pages/registerDetail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { CloudDownloadOutlined } from '@ant-design/icons'
import { loadModule } from '@sentry/utils'
import { Button, Empty, message, notification, Skeleton, Space } from 'antd'
import React, { Fragment, useLayoutEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import { fileDownload } from '../../api/public'
import { downloadCertificate} from '../../api/public'
import { getCompetitionInfo, getCompetitionSignInfo, getTeamInfo, getWorkInfo } from '../../api/user'
import TopBar from '../../components/TopBar'
import './index.scss'
Expand Down Expand Up @@ -189,62 +188,47 @@ function RegisterDetail() {
}
}

const getFileNameFromUrl = (url: string) => {
const urlParts = url.split("/");
return urlParts[urlParts.length - 1];
};
/**
* 文件下载
* @param url 文件url
*/
const downloadFile = (url: string) => {
const downloadFile = async (url: string) => {
const loadingKey='downloading'
message.loading({
content: '正在下载文件',
duration: 500,
duration: 30,
key: 'downloading',
})
fileDownload(url)
.then((res) => {
const content = res.headers['content-disposition']
console.log('content', res)
const fileBlob = new Blob([res.data])
const url = window.URL.createObjectURL(fileBlob)
let filename = 'no-file'
const name1 = content.match(/filename=(.*);/) // 获取filename的值
const name2 = content.match(/filename\*=(.*)/) // 获取filename*的值
// name1 = decodeURIComponent(name1)
// name2 = decodeURIComponent(name2.substring(6)) // 下标6是UTF-8
if (name2 !== null) {
filename = decodeURIComponent(name2[0].substring(17))
} else {
if (name1 !== null) {
filename = decodeURIComponent(name1[0])
} else {
filename = 'no-file'
}
}
if (filename !== 'no-file') {
const a = document.createElement('a')
a.style.display = 'none'
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
message.success({
content: '😁 下载成功',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
const res = await downloadCertificate(url)
console.log(res.data);

const response=res.data
if (response.success) {
const file = await fetch(response.data.url)
const fileBlob = await file.blob()
const urlvalue = window.URL.createObjectURL(fileBlob)
const a = document.createElement('a')
a.style.display = 'none'
a.href = urlvalue
a.download = getFileNameFromUrl(url)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(urlvalue)
message.success({
content: '😁下载完成!'
})
.catch((err) => {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
message.destroy(loadingKey)
}

const changeRegisterInfo = () => {
Expand Down
80 changes: 33 additions & 47 deletions src/pages/reviewApprover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useNavigate, useParams } from 'react-router-dom'
import TopBar from '../../components/TopBar'
import { uploadWorkScoreInfo } from '../../api/judge'
// import Pdf from './components/index'
import { fileDownload } from '../../api/public'
import { downloadCertificate } from '../../api/public'
import { DownloadOutlined } from '@ant-design/icons'

const { Link } = Anchor
Expand Down Expand Up @@ -40,62 +40,48 @@ const ReviewApprover: React.FC = (props) => {

// 提交表单
const navigate = useNavigate()

const getFileNameFromUrl = (url: string) => {
const urlParts = url.split("/");
return urlParts[urlParts.length - 1];
};
/**
* 文件下载
* @param url 文件url
*/
const downloadFile = (url: string) => {
const downloadFile = async (url: string) => {
const loadingKey = 'downloading'
message.loading({
content: '正在下载文件',
duration: 500,
duration: 30,
key: 'downloading',
})
fileDownload(url)
.then((res) => {
const content = res.headers['content-disposition']
console.log('content', res)
const fileBlob = new Blob([res.data])
const url = window.URL.createObjectURL(fileBlob)
let filename = 'no-file'
const name1 = content.match(/filename=(.*);/) // 获取filename的值
const name2 = content.match(/filename\*=(.*)/) // 获取filename*的值
// name1 = decodeURIComponent(name1)
// name2 = decodeURIComponent(name2.substring(6)) // 下标6是UTF-8
if (name2 !== null) {
filename = decodeURIComponent(name2[0].substring(17))
} else {
if (name1 !== null) {
filename = decodeURIComponent(name1[0])
} else {
filename = 'no-file'
}
}
if (filename !== 'no-file') {
const a = document.createElement('a')
a.style.display = 'none'
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
message.success({
content: '😁 下载成功',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
const res = await downloadCertificate(url)
console.log(res.data);

const response = res.data
if (response.success) {
const file = await fetch(response.data.url)
const fileBlob = await file.blob()
const urlvalue = window.URL.createObjectURL(fileBlob)
const a = document.createElement('a')
a.style.display = 'none'
a.href = urlvalue
a.download = getFileNameFromUrl(url)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(urlvalue)
message.success({
content: '😁下载完成!'
})
.catch((err) => {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
message.destroy(loadingKey)
}
// 处理提交事件
const handleSubmit = () => {
Expand Down
80 changes: 33 additions & 47 deletions src/pages/reviewJudge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNavigate, useParams } from 'react-router-dom'
import { uploadWorkJudgeInfo } from '../../api/judge'
import TopBar from '../../components/TopBar'
import './index.scss'
import { fileDownload } from '../../api/public'
import { downloadCertificate } from '../../api/public'
import { DownloadOutlined } from '@ant-design/icons'

const { Link } = Anchor
Expand Down Expand Up @@ -33,62 +33,48 @@ const ReviewJudge: React.FC = (props) => {

// 提交
const navigate = useNavigate()

const getFileNameFromUrl = (url: string) => {
const urlParts = url.split("/");
return urlParts[urlParts.length - 1];
};
/**
* 文件下载
* @param url 文件url
*/
const downloadFile = (url: string) => {
const downloadFile = async (url: string) => {
const loadingKey = 'downloading'
message.loading({
content: '正在下载文件',
duration: 500,
duration: 30,
key: 'downloading',
})
fileDownload(url)
.then((res) => {
const content = res.headers['content-disposition']
console.log('content', res)
const fileBlob = new Blob([res.data])
const url = window.URL.createObjectURL(fileBlob)
let filename = 'no-file'
const name1 = content.match(/filename=(.*);/) // 获取filename的值
const name2 = content.match(/filename\*=(.*)/) // 获取filename*的值
// name1 = decodeURIComponent(name1)
// name2 = decodeURIComponent(name2.substring(6)) // 下标6是UTF-8
if (name2 !== null) {
filename = decodeURIComponent(name2[0].substring(17))
} else {
if (name1 !== null) {
filename = decodeURIComponent(name1[0])
} else {
filename = 'no-file'
}
}
if (filename !== 'no-file') {
const a = document.createElement('a')
a.style.display = 'none'
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
message.success({
content: '😁 下载成功',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
const res = await downloadCertificate(url)
console.log(res.data);

const response = res.data
if (response.success) {
const file = await fetch(response.data.url)
const fileBlob = await file.blob()
const urlvalue = window.URL.createObjectURL(fileBlob)
const a = document.createElement('a')
a.style.display = 'none'
a.href = urlvalue
a.download = getFileNameFromUrl(url)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(urlvalue)
message.success({
content: '😁下载完成!'
})
.catch((err) => {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
} else {
message.error({
content: '😞 下载发生了错误,请联系管理员',
key: 'downloading',
})
}
message.destroy(loadingKey)
}
const handleSubmit = () => {
console.log(isPass);
Expand Down
45 changes: 31 additions & 14 deletions src/pages/workDetail/component/Uploader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UploadOutlined } from '@ant-design/icons'
import { Button, message, Upload } from 'antd'
import React from 'react'
import { uploadWork } from '../../../../api/user'
import { getLicense, uploadWork } from '../../../../api/user'

function Uploader(props: any) {
const localProps: any = {
Expand All @@ -10,19 +10,36 @@ function Uploader(props: any) {
onChange(info: any) {
console.log('onChange', info)
},
// customRequest(options: any) {
// console.log('options', options)
// const { onSuccess, onError, file, onProgress } = options
// uploadWork(Number(props.competitionId), props.inputName, file, onProgress).then((res) => {
// console.log(res)
// if (res.data.errCode === null) {
// onSuccess(res, file)
// message.success({
// content: file.name + ' 上传成功',
// })
// }
// })
// },
customRequest(options: any) {
// console.log('options', options)
// const { onSuccess, onError, file, onProgress } = options
// uploadWork(Number(props.competitionId), props.inputName, file, onProgress).then((res) => {
// console.log(res)
// if (res.data.errCode === null) {
// onSuccess(res, file)
// message.success({
// content: file.name + ' 上传成功',
// })
// }
// })
const temp = options.file as File
getLicense(temp.name, props.inputName, props.competitionId as unknown as number).then(res => {
console.log(res);
const { onSuccess, file } = options
console.log(res.data.data.url);
fetch(res.data.data.url, {
method: "put",
body: temp
}).then(_ => {
onSuccess && onSuccess(res)
message.success({
content: (file as File).name + ' 上传成功',
})
})
}).catch(err => {
console.log(err);
})
},
headers: { Token: localStorage.getItem('approval-system-token') },
}
return (
Expand Down
Loading
Loading