# mt.downloadFile(Object object)
下载文件资源到本地。
# 支持情况
| 项 | 说明 |
|---|---|
| 兼容性 | 部分兼容 |
# 参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| url | string | — | 是 | 下载资源的 URL |
| filePath | string | — | 否 | 指定文件下载后存储的路径;不填时存入临时目录 |
| success | function | — | 否 | 成功回调 |
| fail | function | — | 否 | 失败回调 |
| complete | function | — | 否 | 完成回调(成功或失败均触发) |
success 回调参数
| 属性 | 类型 | 说明 |
|---|---|---|
| tempFilePath | string | 临时文件路径(未指定 filePath 时有值) |
| filePath | string | 用户指定的存储路径(指定了 filePath 时有值) |
| statusCode | number | HTTP 状态码 |
# 返回值
返回 DownloadTask 对象。
# 注意事项
- 未指定
filePath时,相同 URL 的文件会复用本地缓存,不重复下载。 - 指定了
filePath时不使用缓存,每次均重新下载。 - 最大并发下载数为 12,超出后排队等待。
# 示例代码
const task = mt.downloadFile({
url: 'https://example.com/file.png',
success(res) {
console.log(res.tempFilePath)
},
fail(err) {
console.error(err.errMsg)
}
})
task.onProgressUpdate((res) => {
console.log('下载进度:', res.progress)
})
# 与微信差异
- 相同 URL 的文件会命中本地缓存直接返回,微信每次均重新下载。
- 不支持微信的
header参数(自定义请求头),请求头固定附加{ isDownload: 1 }。 - 不支持微信的
timeout参数(超时时间),超时固定为 20000 ms。 - 不支持微信的
enableProfile、enableHttp2、enableQuic参数。 - success 回调不返回
profile字段(微信提供下载性能数据)。