-
-
Notifications
You must be signed in to change notification settings - Fork 13
fix: send headers for file download #355
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
Conversation
} | ||
|
||
export default function DownloadButton(props: DownloadButtonProps) { | ||
function handleAuthenticatedDownload() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make the code easier to understand if a param is used for this method
function handleAuthenticatedDownload() { | |
function handleAuthenticatedDownload(getHeadersFn: GetAuthHeaders) { |
const link = document.createElement('a') | ||
link.href = downloadUrl | ||
link.download = props.fileName | ||
document.body.appendChild(link) | ||
link.click() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use temporaryLink
to make it obvious this will be deleted
return fetch(props.url, { headers }) | ||
}) | ||
.then((response) => { | ||
return response.blob() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified using axios
// Use axios to fetch the file
return axios.get(props.url, {
headers,
responseType: 'blob', // Important to specify 'blob' to handle file downloads
});
})
.then((response) => {
const blob = response.data
// Create a download link and trigger the download
const downloadUrl = URL.createObjectURL(blob)
Changes
Screenshots