微信小程序开发 专题
专题目录
您的位置:微信小程序开发 > 微信小程序开发专题 > 微信小程序API 文件
微信小程序API 文件
作者:--    发布时间:2019-11-20

wx.savefile(object)


保存文件到本地。

object参数说明:

参数类型必填说明
tempfilepathstring需要保存的文件的临时路径
successfunction返回文件的保存路径,res = {savedfilepath: '文件的保存路径'}
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明
savedfilepath文件的保存路径

示例代码:

wx.chooseimage({
  success: function(res) {
    var tempfilepath = res.tempfilepath
    wx.savefile({
      tempfilepath: tempfilepath[0],
      success: function(res) {
        var savedfilepath = res.savedfilepath
      }
    })
  }
})

bug & tip

  1. tip: 本地文件存储的大小限制为 10m

wx.getfileinfo(object)

基础库 1.4.0 开始支持,低版本需做兼容处理

获取文件信息

object参数说明:

参数名类型必填说明
filepathstring本地文件路径
digestalgorithmstring计算文件摘要的算法,默认值 md5,有效值:md5,sha1
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名类型说明
sizenumber文件大小,单位:b
digeststring按照传入的 digestalgorithm 计算得出的的文件摘要
errmsgstring调用结果

示例代码:

wx.getfileinfo({
    success(res) {
        console.log(res.size)
        console.log(res.digest)
    }
})

wx.getsavedfilelist(object)


获取本地已保存的文件列表

object参数说明:

参数类型必填说明
successfunction接口调用成功的回调函数,返回结果见success返回参数说明
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
errmsgstring接口调用结果
filelistobject array文件列表

filelist中的项目说明:

类型说明
filepathstring文件的本地路径
createtimenumber文件的保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
sizenumber文件大小,单位b

示例代码:

wx.getsavedfilelist({
  success: function(res) {
    console.log(res.filelist)
  }
})

wx.getsavedfileinfo(object)

获取本地文件的文件信息。此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 wx.getfileinfo 接口。

object参数说明:

参数类型必填说明
filepathstring文件路径
successfunction接口调用成功的回调函数,返回结果见success返回参数说明
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
errmsgstring接口调用结果
sizenumber文件大小,单位b
createtimenumber文件的保存是的时间戳,从1970/01/01 08:00:00 到当前时间的秒数

示例代码:

wx.getsavedfileinfo({
  filepath: 'wxfile://somefile', //仅做示例用,非真正的文件路径
  success: function(res) {
    console.log(res.size)
    console.log(res.createtime)
  }
})

wx.removesavedfile(object)

删除本地存储的文件

object参数说明:

参数类型必填说明
filepathstring需要删除的文件路径
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.getsavedfilelist({
  success: function(res) {
    if (res.filelist.length > 0){
      wx.removesavedfile({
        filepath: res.filelist[0].filepath,
        complete: function(res) {
          console.log(res)
        }
      })
    }
  }
})

wx.opendocument(object)

新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx

object参数说明:

参数说明必填说明最低版本
filepathstring文件路径,可通过 downfile 获得 
filetypestring文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx1.4.0
successfunction接口调用成功的回调函数 
failfunction接口调用失败的回调函数 
completefunction接口调用结束的回调函数(调用成功、失败都会执行) 

示例代码

wx.downloadfile({  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    var filepath = res.tempfilepath 
    wx.opendocument({
      filepath: filepath,
      success: function (res) {
        console.log('打开文档成功')
      }
    })
  }
})
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册