downloadFile
解释:下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径
方法参数:Object
Object参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
url | String | 是 | 下载资源的 url |
header | Object | 否 | HTTP 请求 Header,header 中不能设置 Referer |
success | Function | 否 | 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: ‘文件的临时路径’} |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
说明:
- 文件的临时路径,在智能小程序本次启动期间可以正常使用,如需持久保存,需再主动调用 swan.saveFile,才能在智能小程序下次启动时访问得到;
- 请在 header 中指定合理的 Content-Type 字段,以保证客户端正确处理文件类型。
- 下载最大限制10MB。
success返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
tempFilePath | String | 临时文件路径,下载后的文件会存储到一个临时文件 |
statusCode | Number | 开发者服务器返回的 HTTP 状态码 |
示例 1在开发者工具中预览效果
- swan.downloadFile({
url: 'https://smartprogram.baidu.com/xxx', //仅为示例,并非真实的资源
success: function (res) {
//下载成功
if (res.statusCode === 200) {
console.log("临时文件路径" + res.tempFilePath);
}
},
fail: function (err) {
console.log('错误码:' + err.errCode);
console.log('错误信息:' + err.errMsg);
}
});
返回值:
返回一个 downloadTask 对象,通过 downloadTask ,可监听下载进度变化事件,以及取消下载任务。
downloadTask 对象的方法列表:
方法 | 类型 | 说明 |
---|---|---|
onProgressUpdate | callback | 监听上传进度变化 |
abort | - | 中断下载任务 |
onProgressUpdate 返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
progress | Number | 下载进度百分比 |
totalBytesWritten | Number | 已经下载的数据长度,单位 Bytes。 |
totalBytesExpectedToWrite | Number | 预期需要下载的数据总长度,单位 Bytes。 |
示例 2
- const downloadTask = swan.downloadFile({
url: 'https://smartprogram.baidu.com/xxx', // 仅为示例,并非真实的资源
success: function (res){
console.log(res.tempFilePath);
},
fail: function (err) {
console.log('错误码:' + err.errCode);
console.log('错误信息:' + err.errMsg);
}
});
downloadTask.onProgressUpdate(res => {
console.log('下载进度', res.progress);
console.log('已经下载的数据长度', res.totalBytesWritten);
console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
});
downloadTask.abort(); // 取消下载任务
说明:
uploadFile 上传文件大小限制为 25M。