MCPcopy Index your code
hub / github.com/simstudioai/sim / triggerFileDownload

Function triggerFileDownload

apps/sim/lib/uploads/client/download.ts:3–26  ·  view source on GitHub ↗
(record: WorkspaceFileRecord)

Source from the content-addressed store, hash-verified

1import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
2
3export async function triggerFileDownload(record: WorkspaceFileRecord): Promise<void> {
4 const isMarkdown =
5 record.type === 'text/markdown' ||
6 record.type === 'text/x-markdown' ||
7 /\.(?:md|markdown)$/i.test(record.name)
8
9 const url = isMarkdown
10 ? `/api/files/export/${encodeURIComponent(record.id)}`
11 : `/api/files/serve/${encodeURIComponent(record.key)}?context=workspace&t=${Date.now()}`
12
13 const response = await fetch(url, { cache: 'no-store' })
14 if (!response.ok) throw new Error(`Failed to download file: ${response.statusText}`)
15
16 const blob = await response.blob()
17 const objectUrl = URL.createObjectURL(blob)
18 const a = document.createElement('a')
19 a.href = objectUrl
20 a.download =
21 response.headers.get('Content-Disposition')?.match(/filename="([^"]+)"/)?.[1] ?? record.name
22 document.body.appendChild(a)
23 a.click()
24 document.body.removeChild(a)
25 URL.revokeObjectURL(objectUrl)
26}

Callers 2

FilesFunction · 0.90
handleDownloadFunction · 0.90

Calls 2

testMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected