【微信小程序开发小白零基础入门】微信小程序文件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天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
15 7
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
16 7
|
8天前
|
小程序 前端开发
微信综合购物商城小程序ui模板源码
微信电商小程序前端页面,综合购物商城ui界面模板。主要功能包含:电商主页、商品分类、购物车、购物车结算、我的个人中心管理、礼券、签到、新人专享、专栏、商品详情页、我的订单、我的余额、我的积分、我的收藏、我的地址、我的礼券等。这是一款非常齐全的电商小程序前端模板。
22 4
|
4天前
|
小程序 开发者
【微信小程序】 微信小程序报错不在以下request合法域名列表中
【微信小程序】 微信小程序报错不在以下request合法域名列表中
10 0
|
4天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的游戏账号交易微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的游戏账号交易微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
4天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的微信智能招聘小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的微信智能招聘小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
4天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的微信社团小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的微信社团小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
10天前
|
小程序 定位技术 API
uniapp 开发微信小程序 --【地图】打开地图选择位置,打开地图显示位置(可开启导航)
uniapp 开发微信小程序 --【地图】打开地图选择位置,打开地图显示位置(可开启导航)
37 0
|
11天前
|
NoSQL 安全 API
如何有效提升 API 接口的安全性?
**API安全关键在于验证和防刷。通过排序参数、生成签名和MD5加密确保请求合法性。使用Redis限制请求频率,防止接口被恶意刷取。验证和防刷策略结合,保护API免受攻击和滥用。**
29 0
|
16天前
|
JSON 安全 API
如何高效编写API接口:以Python与Flask为例
构建RESTful API的简明教程:使用Python的Flask框架,从环境准备(安装Python,设置虚拟环境,安装Flask)到编写首个API(包括获取用户列表和单个用户信息的路由)。运行API服务器并测试在`http://127.0.0.1:5000/users`。进阶话题包括安全、数据库集成、API文档生成和性能优化。【6月更文挑战第27天】
40 7