后浪云百度小程序教程:swan.downloadFile

  • swan.downloadFile
    • 方法参数
      • object 参数说明
      • success 返回参数说明
      • fail 返回参数说明
    • 示例
      • 图片示例
      • 代码示例 1
      • 代码示例 2:指定下载路径
    • Bug & Tip

    swan.downloadFile

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

    方法参数

    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 返回参数说明

    • Android
    错误码说明

    202

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

    1001

    执行错误

    • iOS
    错误码说明

    202

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

    1001

    请求文件超过 50M

    1002

    无法确定下载文件大小

    示例

    跳转编辑工具

    在开发者工具中打开

    在 WEB IDE 中打开

    扫码体验

    代码示例

    请使用百度APP扫码

    图片示例

    代码示例 1

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="display-area">
    4. <view>
    5. <image class="file-icon" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/file-pdf.png" mode="widthFix"></image>
    6. </view>
    7. <view class="tip-week">示例文件.pdf</view>
    8. </view>
    9. <button type="primary" bindtap="downloadFile">下载文件</button>
    10. </view>
    11. </view>

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

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    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

    • Tip:文件的临时路径,在智能小程序本次启动期间可以正常使用,如需持久保存,需再主动调用 swan.saveFile ,才能在智能小程序下次启动时访问得到。
    • Tip:请在 Header 中指定合理的 Content-Type 字段,以保证客户端正确处理文件类型。
    • Tip:下载最大限制 50MB 。
    THE END