微信小程序学习之路——API文件

文件

从网络下载、拍照、录音、录视频时,文件都是存在临时文件中,需要永久保存这些文件就需要我们主动调用API进行保存,这时小程序会将文件保存到系统指定目录,关于这些文件操作都需要调用相关API

wx.saveFile(Object)

保存文件到本地,本地文件存储大小限制为10MB,Object参数属性如下:

属性类型默认值必填说明
tempFilePathstring 需要保存的文件的临时路径
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.startRecord({//开始录音
  success:function(res){
    var tempFilePath = res.tempFilePath//临时文件路径
    wx.saveFile({//保存临时文件
      tempFilePath: 'tempFilePath',
      success:function(res){
        var savedFilePath = res.savedFilePath;//永久地址路径
        console.log('录音文件已保存到:'+savedFilePath);
      }
    })
  }
})

wx.getSavedFileList(Object)

获取本地已保存文件列表,Object参数属性如下:

属性类型默认值必填说明
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.getSavedFileList({
  success:function(res){
    for(var i =0,file;file =res.fileList[i];++i){
      console.log('第'+i+'个文件路径:'+file.filePath);
    }
  }
});

wx.getSavedFileInfo(Object)

获取本地文件的文件信息,Object属性如下:

属性类型默认值必填说明
filePathstring 文件路径
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.getSavedFileList({
  success:function(res){
    for(var i =0,file;file=res.fileList[i];++i){
      wx.getSavedFileInfo({
        filePath: 'file,filePath',
        success:function(res){
          console.log('文件大小为:'+res.size);
        }
      });
    }
  }
});

wx.removeSavedFile(Object)

删除本地存储文件,Object参数属性如下:

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

示例代码如下:

wx.getSavedFileList({
  success:function(res){
    //删除所有文件
    for(var i=0,file;file=res.fileList[i];++i){
      wx.removeSavedFile({
        filePath: 'file.filePath',
      });
    }
  }
});

wx.openDocument(Object)

在新页面中打开文档,支持格式有:doc、docx、xls、xlsx、ppt、pptx、pdf,Object参数属性如下:

属性类型默认值必填说明最低版本
filePathstring 文件路径,可通过 downloadFile 获得 
fileTypestring 文件类型,指定文件类型打开文件1.4.0
successfunction 接口调用成功的回调函数 
failfunction 接口调用失败的回调函数 
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.downloadFile({
  url:'hppt://www.myserver.com/my.docx',
  success:function(res){
    var filePath  =res.filePath;
    //下载文档后在新页面中预览
    wx.openDocument({
      filePath: 'filePath',
    });
  }
});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值