Skip to content

@longmo-utils/browser / triggerDownload

Function: triggerDownload()

ts
function triggerDownload(
   href, 
   fileName, 
   revokeDelay): void;

通用下载触发函数

通过创建临时 <a> 标签并触发点击事件来下载文件,支持自动清理内存

Parameters

ParameterTypeDefault valueDescription
hrefstringundefined文件下载的 URL 或 Blob URL
fileNamestring | undefinedundefined下载文件的名称,如果未提供则使用默认值
revokeDelaynumber100清理 URL 的延迟时间(毫秒),默认为 100ms

Returns

void

Examples

typescript
triggerDownload('https://example.com/file.pdf', 'document.pdf');
typescript
const blob = new Blob(['content'], { type: 'text/plain' });
const blobUrl = URL.createObjectURL(blob);
triggerDownload(blobUrl, 'file.txt', 200);
typescript
triggerDownload('https://example.com/data.json', undefined);

Released under the MIT License.