Fix PDF download: Handle binary file responses in API proxy
This commit is contained in:
parent
d5037b35a9
commit
4abdf9c367
1 changed files with 30 additions and 3 deletions
|
|
@ -71,14 +71,41 @@ async function proxyToBackend(request: NextRequest, pathSegments: string[]) {
|
|||
|
||||
const response = await fetch(url, options);
|
||||
|
||||
// Get response body
|
||||
// Check if this is a file download (binary response)
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
const isFileDownload = contentType.includes('application/pdf') ||
|
||||
contentType.includes('application/octet-stream') ||
|
||||
response.headers.get('content-disposition');
|
||||
|
||||
// Handle binary file downloads differently
|
||||
if (isFileDownload) {
|
||||
const blob = await response.blob();
|
||||
const headers = new Headers();
|
||||
|
||||
// Copy important headers for file downloads
|
||||
if (response.headers.get('content-type')) {
|
||||
headers.set('Content-Type', response.headers.get('content-type')!);
|
||||
}
|
||||
if (response.headers.get('content-disposition')) {
|
||||
headers.set('Content-Disposition', response.headers.get('content-disposition')!);
|
||||
}
|
||||
if (response.headers.get('content-length')) {
|
||||
headers.set('Content-Length', response.headers.get('content-length')!);
|
||||
}
|
||||
|
||||
return new NextResponse(blob, {
|
||||
status: response.status,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
// Handle regular JSON/text responses
|
||||
const data = await response.text();
|
||||
|
||||
// Return response with same status and headers
|
||||
return new NextResponse(data, {
|
||||
status: response.status,
|
||||
headers: {
|
||||
'Content-Type': response.headers.get('content-type') || 'application/json',
|
||||
'Content-Type': contentType || 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue