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

  • swan.getFileInfo
    • 方法参数
      • object 参数说明
      • success 返回参数说明
    • 示例
      • 图片示例
      • 代码示例 1:获取临时文件信息
      • 图片示例
      • 代码示例 2:获取本地文件信息
      • 代码示例 3:digestAlgorithm 为 sha1 ,获取本地文件信息
    • 错误码
      • Android
      • iOS

    swan.getFileInfo

    解释:获取文件信息
    Web 态说明: 受浏览器限制,Web 态不支持文件系统相关功能,调用该方法会执行失败回调函数。

    方法参数

    Object object

    object 参数说明

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

    filePath

    String

    本地文件路径

    digestAlgorithm

    String

    md5

    计算文件摘要的算法,有效值:md5,sha1

    success

    Function

    接口调用成功的回调函数

    fail

    Function

    接口调用失败的回调函数

    complete

    Function

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

    success 返回参数说明

    参数名 类型 说明

    size

    Number

    文件大小(单位:B)

    digest

    String

    按照传入的 digestAlgorithm 计算得出的的文件摘要

    示例

    扫码体验

    代码示例

    后浪云百度小程序教程:swan.getFileInfo插图

    请使用百度APP扫码

    图片示例

    后浪云百度小程序教程:swan.getFileInfo插图1

    代码示例 1:获取临时文件信息

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="display-area">
    4. <image class="display-area-image" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/file-pdf.png" mode="widthFix"></image>
    5. <view class="title">示例文件.pdf</view>
    6. </view>
    7. </view>
    8. <button type="primary" bindtap="getFileInfo">获取服务器上文件信息</button>
    9. </view>

    图片示例

    后浪云百度小程序教程:swan.getFileInfo插图2

    代码示例 2:获取本地文件信息

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="display-area">
    4. <image class="display-area-image" src="/images/file-pdf.png" mode="widthFix"></image>
    5. <view class="title">示例文件.pdf</view>
    6. </view>
    7. <button type="primary" bindtap="getFileInfo">获取本地文件信息</button>
    8. </view>

    代码示例 3:digestAlgorithm 为 sha1 ,获取本地文件信息

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. getFileInfo() {
    3. swan.downloadFile({
    4. header: {
    5. 'Cache-Control': 'no-cache'
    6. },
    7. // 仅为示例,并非真实的接口地址
    8. url: 'https://example.com/file/xxx',
    9. success: res => {
    10. swan.getFileInfo({
    11. filePath: res.tempFilePath,
    12. digestAlgorithm: 'sha1',
    13. success: res => {
    14. console.log('getFileInfo success', res);
    15. swan.showModal({
    16. title: 'getFileInfo success',
    17. content: JSON.stringify(res)
    18. });
    19. },
    20. fail: err => {
    21. console.log('getFileInfo fail', err);
    22. swan.showModal({
    23. title: 'getFileInfo fail',
    24. content: JSON.stringify(err)
    25. });
    26. }
    27. });
    28. }
    29. })
    30. }
    31. })

    错误码

    Android

    错误码 说明

    202

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

    1001

    执行失败

    2001

    文件不存在

    2003

    IO 异常

    iOS

    错误码 说明

    202

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

    2001

    文件不存在

    THE END