小程序文档整理之 -- API(文件)

本文档整理了小程序中关于文件的API,包括wx.saveFile用于保存文件到本地,wx.getFileInfo获取文件信息,wx.getSavedFileList列出已保存的文件,wx.removeSavedFile删除文件,以及wx.openDocument用于打开文档的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件

wx.saveFile(object)

保存文件到本地

wx.chooseImage({
  success: function(res) {
    var tempFilePaths = res.tempFilePaths
    wx.saveFile({
      tempFilePath: tempFilePaths[0],//需要保存的文件的临时路径
      success: function(res) {//返回文件的保存路径
        var savedFilePath = res.savedFilePath//文件的保存路径
      },
      fail/complete: function (res) {//接口调用失败/结束的回调函数
      }
    })
  }
})

注意:本地文件存储的大小限制为 10M

wx.getFileInfo(object)

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

wx.getSavedFileList({
  success: function(res) {
    res.errMsg//接口调用结果
    res.fileList//文件列表
    res.fileList.filePath//文件的本地路径
    res.fileList.createTime//文件的保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
    res.fileList.size//文件大小,单位B
  },
  complete: function (res){//接口调用结束的回调函数(调用成功、失败都会执行)
  }
 })

wx.getSavedFileList(object)

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

wx.getSavedFileInfo({
  filePath: 'wxfile://somefile', //文件路径
  success: function(res) {
    res.errMsg//接口调用结果
    res.createTime//文件的保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
    res.size//文件大小,单位B
  }
  complete: function (res){//接口调用结束的回调函数(调用成功、失败都会执行)
  }
})

wx.getSavedFileInfo(object)

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

wx.getSavedFileInfo({
  filePath: 'wxfile://somefile', //仅做示例用,非真正的文件路径
  success: function(res) {
    res.errMsg//接口调用结果
    res.createTime//文件的保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
    res.size//文件大小,单位B
  }
  complete: function (res){//接口调用结束的回调函数(调用成功、失败都会执行)
  }
})

wx.removeSavedFile(object)

删除本地存储的文件

wx.getSavedFileList({
  success: function(res) {
    if (res.fileList.length > 0){
    //删除本地存储的文件
      wx.removeSavedFile({
        filePath: res.fileList[0].filePath,//需要删除的文件路径
        success/fail/complete: function (res) {//接口调用成功/失败/结束的回调函数
        }
      })
    }
  }
})

wx.openDocument(object)

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

wx.downloadFile({
  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    var filePath = res.tempFilePath
    wx.openDocument({
      filePath: filePath,//(必要)文件路径,可通过 downFile 获得
      fileType://文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
      success/fail/complete: function (res) {//接口调用成功/失败/结束的回调函数
      }
    })
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值