Skip to content

Commit

Permalink
Handle API response when content is a blob on pdf download
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Jan 4, 2024
1 parent 6a517c6 commit 0eb4903
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions app/assets/scripts/components/documents/document-download-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,20 @@ export default function DocumentDownloadMenu(props) {
return;
}

// If we get a 200, it means the PDF is ready for download.
// We get the s3 url and use file saver to download and save the pdf.
if (
response.status === 200 &&
response.headers.get('content-type') === 'application/json'
) {
const result = await response.json();
if (response.status === 200) {
const contentType = response.headers.get('content-type');
let downloadData;

if (contentType === 'application/json') {
const result = await response.json();
downloadData = result.pdf_url;
} else if (contentType === 'application/pdf') {
downloadData = await response.blob();
} else {
throw new Error('Unexpected content type received.');
}

saveAs(result.pdf_url, fileName);
saveAs(downloadData, fileName);
processToast.success(
'PDF downloaded successfully! If the PDF did not open automatically, your browser may have blocked the download. Please make sure that popups are allowed on this site.'
);
Expand Down

0 comments on commit 0eb4903

Please sign in to comment.