简体中文
下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 | 5.0 |
下载文件常见场景是apk的下载更新,app升级中心是一个现成的开源项目,实现下载进度在通知栏显示等复杂交互,可直接使用。
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | DownloadFileOptions | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| tempFilePath | string | 是 | - | 临时文件路径,下载后的文件会存储到一个临时文件 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| statusCode | number | 是 | - | 开发者服务器返回的 HTTP 状态码 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filePath | string | 否 | - | 用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile | DownloadFileSuccessProfile | 否 | - | 需要基础库: 2.10.4网络请求过程中一些调试信息,查看详细说明 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | - | 错误码 | ||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||
| data | any | 否 | - | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | - | |||||||||||||||||||||||||||||||
uni.env.CACHE_PATH/cache/uni-download。但在HBuilderX 3.99前有过几次变更,3.98的目录是uni.env.CACHE_PATH/cache/uniDownloads,而3.98之前则不在cache目录下。| 类型 |
|---|
| DownloadTask |
abort 中断下载任务
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 | 5.0 |
onProgressUpdate 监听下载进度变化。
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (result: OnProgressDownloadResult) => void | 是 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| progress | number | 是 | - | 下载进度百分比 | |
| totalBytesWritten | number | 是 | - | 已经下载的数据长度,单位 Bytes | |
| totalBytesExpectedToWrite | number | 是 | - | 预期需要下载的数据总长度,单位 Bytes |
注意事项
complete回调中置空Task对象,例complete: () => {
this.task = null
},
如不释放,在调用Task对象的方法将导致控制台报错:
error: instance object does not exist: id:15
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view>
<page-head :title="data.title"></page-head>
<view>
<view v-if="data.imageSrc">
<image class="img" :src="data.imageSrc" mode="aspectFit" />
</view>
<view v-else style="margin: 10px;">
<text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text>
<button type="primary" @tap="downloadImage">下载</button>
</view>
<view style="margin: 10px;">
<text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text>
<button type="primary" @tap="downloadErrorFilename">下载</button>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
import {
testInovkeDownloadFile,
CommonOptions
} from '@/uni_modules/test-invoke-network-api'
// #endif
type DataType = {
title: string;
imageSrc: string;
task: DownloadTask | null;
jest_result: boolean;
jest_callback_triggred: boolean;
}
const data = reactive({
title: 'downloadFile',
imageSrc: '',
task: null,
//自动化测试例专用
jest_result: false,
jest_callback_triggred: false
} as DataType)
onUnload(() => {
// data.imageSrc = '';
uni.hideLoading();
data.task?.abort();
})
const downloadImage = () => {
uni.showLoading({
title: '下载中'
})
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
success: (res) => {
console.log('downloadFile success, res is', res.tempFilePath)
data.imageSrc = res.tempFilePath;
},
fail: (err) => {
console.log('downloadFile fail, err is:', err)
},
complete: (res) => {
uni.hideLoading();
data.task = null;
}
});
data.task?.onProgressUpdate((update) => {
console.log("progress : ", update.progress);
})
}
const downloadErrorFilename = () => {
data.task = uni.downloadFile({
url:"https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/file/test9.txt",
success: (res) => {
console.log('downloadFile success, res is', res.tempFilePath)
},
fail: (err) => {
console.log('downloadFile fail, err is:', err)
}
})
}
//自动化测试例专用
const jest_downloadFile = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
success: () => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false
data.jest_callback_triggred = true
}
});
}
const jest_downloadFile_with_uni_env = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
filePath: `${uni.env.CACHE_PATH}/a/b/`,
success: () => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false
data.jest_callback_triggred = true
}
});
}
const jest_cookie_download = (needCookie : boolean) => {
data.task = uni.downloadFile({
url: "https://request.dcloud.net.cn/api/http/header/download",
success: () => {
data.jest_result = needCookie ? true : false;
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = needCookie ? false : true;
}
});
}
const jest_set_cookie = () => {
uni.request({
url: "https://request.dcloud.net.cn/api/http/header/setCookie",
method: "GET",
timeout: 6000,
sslVerify: false,
withCredentials: true,
firstIpv4: false,
success: () => {
jest_cookie_download(true)
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
},
});
}
const jest_delete_cookie = () => {
uni.request({
url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
method: "GET",
timeout: 6000,
sslVerify: false,
withCredentials: true,
firstIpv4: false,
success: () => {
jest_cookie_download(false)
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
},
});
}
const jest_uts_module_invoked = () => {
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
testInovkeDownloadFile({
success: (res : any) => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: (err : any) => {
data.jest_result = false
data.jest_callback_triggred = true
}
} as CommonOptions)
// #endif
}
const jest_special_characters_download = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
success: () => {
data.jest_result = true;
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
}
});
}
const jest_download_call_timeout = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/video/sample/2minute-demo-10k.mp4",
timeout: 3000,
fail: () => {
data.jest_result = false;
}
})
setTimeout(() => {
data.jest_result = true;
}, 4000)
}
defineExpose({
data,
jest_downloadFile,
jest_downloadFile_with_uni_env,
jest_set_cookie,
jest_delete_cookie,
jest_uts_module_invoked,
jest_special_characters_download,
jest_download_call_timeout
})
</script>
<style>
.img {
margin: 0 auto;
}
</style>
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |