【微信小程序开发小白零基础入门】微信小程序文件API【建议收藏】

简介: 微信小程序文件API文章目录微信小程序文件API一、 保存文件二、 获取文件信息三、 获取本地文件列表四、 获取本地文件信息五、 删除本地文件六、 打开文档七、 推荐小程序(欢迎各位大佬指导)一、 保存文件小程序使用wx.saveFile(OBJECT)保存文件到本地。注意:saveFile会把临时文件移动,因此调用成功后传入的tempFilePath将不可用。其OBJECT参数说明如表所示。参数 类型 必填 说明tempFilePath String 是 需要保存的文件的临时路径success Function 否 返回文件的保存路径,res = {savedFiFile(OBJECT)保存

微信小程序文件API


文章目录


一、 保存文件

小程序使用wx.saveFile(OBJECT)保存文件到本地。注意:saveFile会把临时文件移动,因此调用成功后传入的tempFilePath将不可用。其OBJECT参数说明如表所示。

5ac9301f196b761582597e419a0a7dd.png

例题

js文件

 data: {
    src: '' //图片临时地址
  },
  //下载文件
  downloadFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center',
      success: function (res) {
        if (res.statusCode === 200) {
          that.setData({
            src: res.tempFilePath
          })
        }
      }
    })
  },
  //保存文件
  saveFile: function () {
    var that = this
    let src = this.data.src
    if (src == '') {
      wx.showToast({
        title: '请先下载文件!',
        icon: 'none'
      })
    } else {
      wx.saveFile({
        tempFilePath: src,
        success: function (res) {
          console.log('文件被保存到:' + res.savedFilePath)
          wx.showToast({
            title: '保存成功!'
          })
        }
      })
    }
  },

json文件

{"navigationBarTitleText": "智慧云工具箱-微信小程序文件API"
}

wxml文件

<view class='page-body'>
    <view class='title'>1. 保存文件的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)下载文件</view>
      <button type="primary" bindtap="downloadFile">下载文件</button>
      <image wx:if='{{src}}' src='{{src}}' mode='widthFix'></image>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)保存文件</view>
      <button type="primary" bindtap="saveFile">保存文件</button>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}

0ba97c7273ca46e6a17e630d4050e62c.png


c88c98d4353341cab2aa5e888c6ec92c.png0daf2f9e0db24711a59351643a764dac.png011dcdc8adcc412684fe6d358d61fce4.png

二、 获取文件信息

小程序使用wx.getFileInfo(OBJECT)获取文件信息,该接口从基础库 1.4.0 开始支持,低版本需做兼容处理。OBJECT参数说明如表所示。

a1e48595827837a9dcefa5a4761024b.png

例题

js文件

 data: {
    tempFilePath: '' //临时文件路径
  },
  //下载文件
  downloadFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center', //用户可以更改
      success: function (res) {
        // 只要服务器有响应数据,就会进入 success 回调
        if (res.statusCode === 200) {
          console.log('文件被下载到:' + res.tempFilePath)
          that.setData({
            tip1: '提示:文件已下载。',
            tempFilePath: res.tempFilePath
          })
        }
      }
    })
  },
  //获取临时文件信息
  getFileInfo: function () {
    var that = this
    let tempFilePath = this.data.tempFilePath
    if (tempFilePath == '') {
      //文件尚未保存到本地
      wx.showModal({
        title: '提示',
        content: '请先下载文件!',
        showCancel: false
      })
    } else {
      //获取保存的文件信息
      wx.getFileInfo({
        filePath: tempFilePath,
        success: function (res) {
          that.setData({
            tip2: '文件大小:' + res.size + '字节。'
          })
        }
      })
    }
  },

wxml文件

  <view class='page-body'>
    <view class='title'>2. 获取临时文件信息的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)下载文件</view>
      <button type="primary" bindtap="downloadFile">下载文件</button>
      <view class='title'>{{tip1}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)获取临时文件信息</view>
      <button type="primary" bindtap="getFileInfo">获取文件信息</button>
      <view class='title'>{{tip2}}</view>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}

2ce507be834f4c8dbcdba3b30583e84e.png90b98d9f5e384d20b089fff8a650cbfe.png8920c0377b3e47d1af28e824b0fc8c5d.png8920c0377b3e47d1af28e824b0fc8c5d.png91ade66d81334922988e71875d227dc5.png

三、 获取本地文件列表

小程序使用wx.getSavedFileList(OBJECT)获取本地已保存的文件列表。 OBJECT参数说明如表所示。

参数 类型 必填 说明
success Function 接口调用成功的回调函教
fail Function 接口调用失败的回调函教
complete Function 接口调用结束的回调函数〔调用成功与否都执行)

例题

js文件

 data: {
    savedFilePath: '' //本地文件路径
  },
  //下载和保存文件
  saveFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center', //用户可以更改
      success: function (res) {
        // 只要服务器有响应数据,就会进入 success 回调
        if (res.statusCode === 200) {
          //保存文件到本地
          wx.saveFile({
            tempFilePath: res.tempFilePath,
            success: function (res) {
              console.log('文件保存成功!')
              that.setData({
                tip1: '提示:文件已保存。',
                savedFilePath: res.savedFilePath
              })
            }
          })
        }
      }
    })
  },
  //获取本地文件列表
  getSavedFileList:function(){
    var that = this
    wx.getSavedFileList({
      success: function (res) {
        console.log(res.fileList)
        that.setData({
          tip2: '提示:文件列表已获取。'
        })
      }
    })
  },

json文件

{"navigationBarTitleText": "智慧云工具箱-微信小程序文件API"
}

wxml文件

<view class='page-body'>
    <view class='title'>3. 获取本地文件列表的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)保存文件</view>
      <button type="primary" bindtap="saveFile">保存文件</button>
      <view class='title'>{{tip1}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)获取本地文件列表</view>
      <button type="primary" bindtap="getSavedFileList">获取文件列表</button>
      <view class='title'>{{tip2}}</view>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}
0e1525a8541b40d39cf5d619657542f3.png de3fed878560409eb1d160741876cde2.png ad6e9dc1ad6e42e2a0441e6d556519f5.png

四、 获取本地文件信息

小程序使用wx.getSavedFileInfo(OBJECT)获取本地文件的文件信息。此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 wx.getFileInfo 接口。 OBJECT参数说明如表所示。

参数 类型 必填 说明
filePath String 文件路径
success Function 接口调用成功的回调函数。返回结果见success返回参教说明
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功与否都执行)

例题

js文件

 data: {
    tempFilePath: '', //临时文件路径
    savedFilePath: '' //本地文件路径
  },
  //下载文件
  downloadFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center', //用户可以更改
      success: function (res) {
        // 只要服务器有响应数据,就会进入 success 回调
        if (res.statusCode === 200) {
          console.log('文件被下载到:' + res.tempFilePath)
          that.setData({
            tip1: '提示:文件已下载。',
            tempFilePath: res.tempFilePath
          })
        }
      }
    })
  },
  //保存文件
  saveFile: function () {
    var that = this
    let tempFilePath = this.data.tempFilePath
    if (tempFilePath == '') {
      //文件尚未下载
      wx.showModal({
        title: '提示',
        content: '请先下载文件!',
        showCancel: false
      })
    } else {
      //保存文件到本地
      wx.saveFile({
        tempFilePath: tempFilePath,
        success: function (res) {
          console.log('文件被保存到:' + res.savedFilePath)
          that.setData({
            tip2: '提示:文件已保存。',
            savedFilePath: res.savedFilePath
          })
        }
      })
    }
  },
  //获取文件信息
  getSavedFileInfo: function () {
    var that = this
    let savedFilePath = this.data.savedFilePath
    if (savedFilePath == '') {
      //文件尚未保存到本地
      wx.showModal({
        title: '提示',
        content: '请先保存文件!',
        showCancel: false
      })
    } else {
      //获取保存的文件信息
      wx.getSavedFileInfo({
        filePath: savedFilePath,
        success: function (res) {
          that.setData({
            tip3: '文件大小:' + res.size + '字节。'
          })
        }
      })
    }
  },

json文件

{"navigationBarTitleText": "智慧云工具箱-微信小程序文件API"
}

wxml文件

<view class='container'>
  <include src='../../common/head.wxml' />
  <view class='page-body'>
    <view class='title'>4. 获取本地文件信息的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)下载文件</view>
      <button type="primary" bindtap="downloadFile">下载文件</button>
      <view class='title'>{{tip1}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)保存文件</view>
      <button type="primary" bindtap="saveFile">保存文件</button>
      <view class='title'>{{tip2}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(3)获取本地文件信息</view>
      <button type="primary" bindtap="getSavedFileInfo">获取文件信息</button>
      <view class='title'>{{tip3}}</view>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}

6921c73cd590421d8588343b9b287298.png0298ebd18b484af0932d4a47523e9c1d.png4b0dba94a1b543eaa0d4738ec54e4831.png8af8e8e355ee4c4a9d0d403361f966f0.png98a3444d92a9498fa75b05de56627852.png

五、 删除本地文件

小程序使用wx.removeSavedFile(OBJECT)删除本地已保存的文件。 OBJECT参数说明如

表所示。

参数 类型 必填 说明
filePath String 需要删除的文件路径
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函教
complete Function 接口调用结束的回调函数(调用成功与否都执行)

例题

js文件

 data: {
    savedFilePath: '' //本地文件路径
  },
  //下载和保存文件
  saveFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center', //用户可以更改
      success: function (res) {
        // 只要服务器有响应数据,就会进入 success 回调
        if (res.statusCode === 200) {
          console.log('文件被下载到:' + res.tempFilePath)
          //保存文件到本地
          wx.saveFile({
            tempFilePath: res.tempFilePath,
            success: function (res) {
              console.log('文件被保存到:' + res.savedFilePath)
              that.setData({
                tip1: '提示:文件已保存。',
                savedFilePath: res.savedFilePath
              })
            }
          })
        }
      }
    })
  },
  //删除文件
  removeFile: function () {
    var that = this
    let savedFilePath = this.data.savedFilePath
    if (savedFilePath == '') {
      //文件尚未保存
      wx.showModal({
        title: '提示',
        content: '请先下载和保存文件!',
        showCancel: false
      })
    } else {
      //删除本地文件
      wx.removeSavedFile({
        filePath: savedFilePath,
        success: function (res) {
          that.setData({
            tip2: '提示:文件已被删除。'
          })
        }
      })
    }
  },
  //获取文件信息
  getSavedFileInfo: function () {
    var that = this
    let savedFilePath = this.data.savedFilePath
    //获取保存的文件信息
    wx.getSavedFileInfo({
      filePath: savedFilePath,
      success: function (res) {
        that.setData({
          tip3: '文件大小:' + res.size + '字节。'
        })
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          tip3: '提示:文件不存在。'
        })
      }
    })
  },

json文件

{"navigationBarTitleText": "智慧云工具箱-微信小程序文件API"
}

wxml文件

 <view class='page-body'>
    <view class='title'>5. 删除已保存文件的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)下载并保存文件</view>
      <button type="primary" bindtap="saveFile">下载并保存文件</button>
      <view class='title'>{{tip1}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)删除文件</view>
      <button type="primary" bindtap="removeFile">删除文件</button>
      <view class='title'>{{tip2}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(3)获取本地文件信息</view>
      <button type="primary" bindtap="getSavedFileInfo">获取文件信息</button>
      <view class='title'>{{tip3}}</view>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}
9ce790a909964458bf6a712904921fd0.png f4801e4be0a949c4a89db9524eeec2e6.png 2cd670974156417f90f7d401a811f706.png abecebd2a2664af394ffe071955e8133.png 5470dbe9370541a69d26597d487b3cc4.png

六、 打开文档

小程序使用wx.openDocument(OBJECT)新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。OBJECT参数说明如表所示。

4db667a17c843047634494e043a4d36.png

例题

js文件

data: {
    path: ''
  },
  //下载文件
  downloadFile: function () {
    var that = this
    wx.downloadFile({
      url: 'https://ucc.alicdn.com/images/user-upload-01/2c838b6cb29e4d9dbbbc1197edc83c36.jpg#pic_center', //用户可以更改
      success: function (res) {
        // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          console.log(res)
          that.setData({
            tip: '提示:文件已下载',
            path: res.tempFilePath
          })
        }
      }
    })
  },
  //打开文件
  openDocument: function () {
    let path = this.data.path
    //文档尚未下载
    if (path == '') {
      wx.showModal({
        title: '提示',
        content: '请先下载文档!',
        showCancel: false
      })
    }
    //打开文档
    else {
      wx.openDocument({ filePath: path })
    }
  },

json文件

{"navigationBarTitleText": "智慧云工具箱-微信小程序文件API"
}

wxml文件

  <view class='page-body'>
    <view class='title'>6. 打开文档的简单应用</view>
    <view class='demo-box'>
      <view class='title'>(1)下载文件</view>
      <button type="primary" bindtap="downloadFile">下载文件</button>
      <view class='title'>{{tip}}</view>
    </view>
    <view class='demo-box'>
      <view class='title'>(2)打开文件</view>
      <button type="primary" bindtap="openDocument">打开文件</button>
    </view>
  </view>

wxss文件

button{
  margin: 15rpx;
}

282988d2c4a24a14b31d9842ae696cb4.pngbffd8c2995a94b68a4cb016d8f6a7c3e.png

七、 推荐小程序(欢迎各位大佬指导)

相关文章
|
3月前
|
移动开发 小程序 前端开发
小程序开发平台有哪些?哪个好
小程序的开发方式丰富多元,开发团队可根据自身的技术背景、项目具体需求以及资源状况,灵活挑选最为适宜的开发路径。以下将详细介绍几种主流的小程序开发方式。
383 2
|
3月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
3月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
4月前
|
数据可视化 测试技术 API
从接口性能到稳定性:这些API调试工具,让你的开发过程事半功倍
在软件开发中,接口调试与测试对接口性能、稳定性、准确性及团队协作至关重要。随着开发节奏加快,传统方式已难满足需求,专业API工具成为首选。本文介绍了Apifox、Postman、YApi、SoapUI、JMeter、Swagger等主流工具,对比其功能与适用场景,并推荐Apifox作为集成度高、支持中文、可视化强的一体化解决方案,助力提升API开发与测试效率。
|
3月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
4月前
|
JSON 前端开发 API
如何调用体育数据足篮接口API
本文介绍如何调用体育数据API:首先选择可靠服务商并注册获取密钥,接着阅读文档了解基础URL、端点、参数及请求头,然后使用Python等语言发送请求、解析JSON数据,最后将数据应用于Web、App或分析场景,同时注意密钥安全、速率限制与错误处理。
503 152
|
5月前
|
JSON 算法 安全
淘宝商品详情API接口系列,json数据返回
淘宝开放平台提供了多种API接口用于获取商品详情信息,主要通过 淘宝开放平台(Taobao Open Platform, TOP) 的 taobao.tbk.item.info.get(淘宝客商品详情)或 taobao.item.get(标准商品API)等接口实现。以下是关键信息及JSON返回示例:
|
3月前
|
人工智能 自然语言处理 测试技术
Apipost智能搜索:只需用业务语言描述需求,就能精准定位目标接口,API 搜索的下一代形态!
在大型项目中,API 数量庞大、命名不一,导致“找接口”耗时费力。传统工具依赖关键词搜索,难以应对语义模糊或命名不规范的场景。Apipost AI 智能搜索功能,支持自然语言查询,如“和用户登录有关的接口”,系统可理解语义并精准匹配目标接口。无论是新人上手、模糊查找还是批量定位,都能大幅提升检索效率,降低协作成本。从关键词到语义理解,智能搜索让开发者少花时间找接口,多专注核心开发,真正实现高效协作。

热门文章

最新文章