swan.downloadFile
解释:下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。
方法参数
Object object
object参数说明 :
success 返回参数说明 :
fail 返回参数说明 :
- -
示例
扫码体验
请使用百度APP扫码
图片示例
代码示例1 :
在开发者工具中预览效果 -
Page({ data: { }, downloadFile() { swan.downloadFile({ url: 'https://smartprogram.baidu.com/docs/img/file-simple.pdf', header: { 'content-type': 'application/json' }, success: res => { //下载成功 if (res.statusCode === 200) { console.log("临时文件路径" + res.tempFilePath); } }, fail: err => { console.log('错误码:' + err.errCode); console.log('错误信息:' + err.errMsg); } }); }})
代码示例2 - 指定下载路径 :
在开发者工具中预览效果 -
Page({ data: { }, downloadFile() { swan.downloadFile({ url: 'https://smartprogram.baidu.com/docs/img/file-simple.pdf', header: { 'content-type': 'application/json' }, filePath: 'bdfile://usr/办理指南文档.pdf', success: res => { console.log(res); let filePath = res.filePath; swan.showModal({ title: '文件下载完成', content: '是否需要打开?', confirmText: '打开', success: res => { if (res.confirm) { swan.openDocument({ filePath: filePath, fileType: 'pdf', fail: err => { this.toast('打开失败'); } }); } } }); }, fail: err => { this.toast('下载文件失败'); }, complete: () => { swan.hideToast(); } }); }});
返回值: 返回一个 downloadTask 对象,通过 downloadTask,可监听下载进度变化事件,以及取消下载任务。
Bug & Tip
- -
