---
title: "triggerDownload"
description: "触发浏览器文件下载，支持 URL 和 Blob 作为来源，可自定义下载文件名。"
seo_title: "triggerDownload"
seo_description: "Trigger a browser file download from a Blob with a chosen filename, wiring up the temporary anchor and object URL for you."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/file/trigger-download"
---
# triggerDownload

> 触发浏览器文件下载，支持 URL 和 Blob 作为来源，可自定义下载文件名。

## 用法

`triggerDownload` 函数用于触发浏览器下载文件。

> \[\!WARNING\]
> 
> 注意
> 
> - 仅在浏览器环境中可用
> - 某些浏览器可能会拦截自动下载

```ts
import { triggerDownload } from '@movk/core'

// 下载文本文件
const textBlob = new Blob(['Hello, World!'], { type: 'text/plain' })
triggerDownload(textBlob, 'hello.txt')

// 下载 JSON 数据
const data = { name: 'John', age: 30 }
const jsonBlob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' })
triggerDownload(jsonBlob, 'data.json')

// 下载图片
const canvas = document.createElement('canvas')
canvas.toBlob((blob) => {
  if (blob) {
    triggerDownload(blob, 'image.png')
  }
})
```

## API

### `triggerDownload(blob, filename)`

触发浏览器下载文件。

### 参数

**blob** (`Blob`) *required*: 文件数据。

**filename** (`string`) *required*: 文件名。

## Changelog

See commit history for [src/helpers/file/triggerDownload.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/file/triggerDownload.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
