I'm having trouble finding a solution for downloading files on iPhone Safari.
The code I shared is the current function in my application. It downloads the file correctly in Google Chrome, but it does not work in Safari.
How can I fix this so the file download works on Safari as well?
const downloadDocument = async (url) => {
try {
const response = await fetch(url)
const blob = await response.blob()
const blobUrl = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = blobUrl
link.target = '_blank'
link.download = url.split('/').pop() || 'document'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
setTimeout(() => window.URL.revokeObjectURL(blobUrl), 100)
} catch (error) {
console.error('Error downloading document.:', error)
window.open(url, '_blank', 'noopener,noreferrer')
}
}