13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联百度小程序教程:swan.downloadFile

swan.downloadFile

请参考使用注意事项进行开发。
解释:下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。

创新互联建站于2013年成立,是专业互联网技术服务公司,拥有项目成都做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元南川做网站,已为上家服务,为南川各地企业和个人服务,联系电话:18980820575

方法参数

Object object

object 参数说明

属性名 类型 必填 默认值 说明

url

String

下载资源的 url

header

Object

HTTP 请求 Header ,Header 中不能设置 Referer

filePath

String

指定文件下载后存储的路径

success

Function

下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: ‘文件的临时路径’}

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数 类型 说明

tempFilePath

String

临时文件路径,下载后的文件会存储到一个临时文件

statusCode

Number

开发者服务器返回的 HTTP 状态码

fail 返回参数说明

错误码 说明

202

解析失败,请检查参数是否正确

1001

执行错误

错误码 说明

202

解析失败,请检查参数是否正确

1001

请求文件超过 50M

1002

无法确定下载文件大小

示例

跳转编辑工具

在开发者工具中打开

在 WEB IDE 中打开

扫码体验

代码示例

请使用百度APP扫码

图片示例

代码示例 1

 
 
 
  1. 示例文件.pdf

代码示例 2:指定下载路径

在开发者工具中打开

在开发者工具中打开

在 WEB IDE 中打开

 
 
 
  1. Page({
  2. onLoad(e) {
  3. console.log('建议在真机查看效果');
  4. },
  5. downloadFile() {
  6. this.toast('正在保存', 'loading');
  7. swan.downloadFile({
  8. url: 'https://b.bdstatic.com/miniapp/assets/docs/sprddemofile-sample.pdf',
  9. header: {
  10. 'content-type': 'application/json'
  11. },
  12. filePath: 'bdfile://usr/办理指南文档.pdf',
  13. success: res => {
  14. let filePath = res.filePath;
  15. swan.showModal({
  16. title: '文件下载完成',
  17. content: '是否需要打开?',
  18. confirmText: '打开',
  19. success: res => {
  20. if (res.confirm) {
  21. swan.openDocument({
  22. filePath: filePath,
  23. fileType: 'pdf',
  24. success: res => {
  25. console.log('openDocument', res)
  26. },
  27. fail: err => {
  28. console.log('openDocument', err)
  29. this.toast('打开失败');
  30. }
  31. });
  32. }
  33. }
  34. });
  35. },
  36. fail: err => {
  37. this.toast('下载文件失败');
  38. },
  39. complete: () => {
  40. swan.hideToast();
  41. }
  42. });
  43. },
  44. toast(title, icon = 'none') {
  45. swan.showToast({title, icon});
  46. }
  47. });

返回值
返回一个 downloadTask 对象,通过 downloadTask ,可监听下载进度变化事件,以及取消下载任务。

Bug & Tip


当前文章:创新互联百度小程序教程:swan.downloadFile
文章源于:http://cdbrznjsb.com/article/dpisidj.html

其他资讯

让你的专属顾问为你服务