fix: fix file download error
This commit is contained in:
parent
a7ea7ce4bc
commit
9801ac4c44
@ -1,33 +1,73 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { NextResponse } from 'next/server'
|
import { NextResponse } from 'next/server'
|
||||||
|
|
||||||
|
// export async function GET(request: Request) {
|
||||||
|
// const { searchParams } = new URL(request.url)
|
||||||
|
// const encodeFileNo = searchParams.get('encodeFileNo')
|
||||||
|
// const srcFileNm = searchParams.get('srcFileNm')
|
||||||
|
|
||||||
|
// if (!encodeFileNo) {
|
||||||
|
// return NextResponse.json({ error: 'encodeFileNo is required' }, { status: 400 })
|
||||||
|
// }
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/file/downloadFile2`, {
|
||||||
|
// params: {
|
||||||
|
// encodeFileNo,
|
||||||
|
// },
|
||||||
|
// responseType: 'arraybuffer',
|
||||||
|
// })
|
||||||
|
|
||||||
|
// if (response.headers['content-type'] === 'text/html;charset=utf-8') {
|
||||||
|
// return NextResponse.json({ error: 'file not found' }, { status: 404 })
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const contentType = response.headers['content-type'] || 'application/octet-stream'
|
||||||
|
// const contentDisposition = response.headers['content-disposition'] || 'inline'
|
||||||
|
|
||||||
|
// return new NextResponse(response.data, {
|
||||||
|
// status: 200,
|
||||||
|
// headers: {
|
||||||
|
// 'Content-Type': contentType,
|
||||||
|
// 'Content-Disposition': contentDisposition,
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
// } catch (error: any) {
|
||||||
|
// console.error('File download error:', error)
|
||||||
|
// return NextResponse.json({ error: error.response?.data || 'Failed to download file' }, { status: 500 })
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const { searchParams } = new URL(request.url)
|
const { searchParams } = new URL(request.url)
|
||||||
const encodeFileNo = searchParams.get('encodeFileNo')
|
const encodeFileNo = searchParams.get('encodeFileNo')
|
||||||
|
const srcFileNm = searchParams.get('srcFileNm') || 'downloaded-file'
|
||||||
const srcFileNm = searchParams.get('srcFileNm')
|
|
||||||
|
|
||||||
if (!encodeFileNo) {
|
if (!encodeFileNo) {
|
||||||
return NextResponse.json({ error: 'encodeFileNo is required' }, { status: 400 })
|
return NextResponse.json({ error: 'encodeFileNo is required' }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const url = `${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/file/downloadFile2?encodeFileNo=${encodeFileNo}`
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/file/downloadFile2`, {
|
const resp = await fetch(url)
|
||||||
responseType: 'arraybuffer',
|
|
||||||
params: {
|
if (!resp.ok) {
|
||||||
encodeFileNo,
|
return NextResponse.json({ error: 'Failed to download file' }, { status: 500 })
|
||||||
},
|
|
||||||
})
|
|
||||||
if (response.headers['content-type'] === 'text/html;charset=utf-8') {
|
|
||||||
return NextResponse.json({ error: 'file not found' }, { status: 404 })
|
|
||||||
}
|
}
|
||||||
return new NextResponse(response.data, {
|
|
||||||
|
const contentType = resp.headers.get('content-type') || 'application/octet-stream'
|
||||||
|
const contentDisposition = resp.headers.get('content-disposition') || `attachment; filename="${srcFileNm}"`
|
||||||
|
|
||||||
|
return new NextResponse(resp.body, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/octet-stream;charset=UTF-8',
|
'Content-Type': contentType,
|
||||||
'Content-Disposition': `attachment; filename="${srcFileNm}"`,
|
'Content-Disposition': contentDisposition,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return NextResponse.json({ error: error.response.data }, { status: 500 })
|
console.error('File download error:', error)
|
||||||
|
return NextResponse.json({ error: error.response?.data || 'Failed to download file' }, { status: 500 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,19 +76,22 @@ export function useInquiry(
|
|||||||
|
|
||||||
const downloadFile = async (encodeFileNo: number, srcFileNm: string) => {
|
const downloadFile = async (encodeFileNo: number, srcFileNm: string) => {
|
||||||
try {
|
try {
|
||||||
const resp = await axiosInstance(null).get<Blob>(`/api/qna/file`, { params: { encodeFileNo, srcFileNm } })
|
const resp = await fetch(`/api/qna/file?encodeFileNo=${encodeFileNo}&srcFileNm=${srcFileNm}`)
|
||||||
const blob = new Blob([resp.data], { type: 'application/octet-stream;charset=UTF-8' })
|
|
||||||
|
const blob = await resp.blob()
|
||||||
const url = URL.createObjectURL(blob)
|
const url = URL.createObjectURL(blob)
|
||||||
const a = document.createElement('a')
|
const a = document.createElement('a')
|
||||||
a.href = url
|
a.href = url
|
||||||
a.download = `${srcFileNm}`
|
a.download = srcFileNm
|
||||||
|
document.body.appendChild(a)
|
||||||
a.click()
|
a.click()
|
||||||
|
document.body.removeChild(a)
|
||||||
URL.revokeObjectURL(url)
|
URL.revokeObjectURL(url)
|
||||||
|
|
||||||
return blob
|
return blob
|
||||||
} catch (error: any) {
|
} catch (error) {
|
||||||
if (error.response.status === 404) {
|
console.error('File download error:', error)
|
||||||
alert('ファイルが見つかりません')
|
alert('ファイルのダウンロードに失敗しました')
|
||||||
}
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user