({ body, contentType, responseType, filename, uri }: DownloadOptions)
| 32 | } |
| 33 | |
| 34 | export const downloadFile = ({ body, contentType, responseType, filename, uri }: DownloadOptions): void => { |
| 35 | let content = body |
| 36 | |
| 37 | // Decode the body if needed. |
| 38 | if (responseType === 'longText' || responseType === 'stream') { |
| 39 | content = decodeBase64ToUnicode(content) |
| 40 | } else if (responseType === 'text') { |
| 41 | try { |
| 42 | content = JSON.stringify(JSON.parse(content), null, 4) |
| 43 | } catch { |
| 44 | // Fallback to raw content on parsing error. |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Create a Blob URL for downloading. |
| 49 | const blobUrl = contentType.startsWith('image') ? uri : createBlobUrl(content, contentType) |
| 50 | const downloadFilename = decodeURI(filename || 'default_name') |
| 51 | |
| 52 | // Create a temporary anchor element for downloading. |
| 53 | const anchor = document.createElement('a') |
| 54 | anchor.style.display = 'none' |
| 55 | anchor.href = blobUrl |
| 56 | anchor.download = downloadFilename |
| 57 | document.body.appendChild(anchor) |
| 58 | anchor.click() |
| 59 | document.body.removeChild(anchor) |
| 60 | |
| 61 | // Revoke the Blob URL to free resources. |
| 62 | URL.revokeObjectURL(blobUrl) |
| 63 | } |
| 64 | |
| 65 | export const downloadFileFromText = (fileName: string, text: string) => { |
| 66 | const file = new Blob([text], { type: 'data:text/plain;charset=utf-8' }) |
no test coverage detected