【微信小程序开发小白零基础入门】微信小程序文件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天前
|
小程序 前端开发 测试技术
微信小程序的开发完整流程是什么?
微信小程序的开发完整流程是什么?
27 7
|
2天前
|
存储 API 数据库
深入浅出后端开发:从零到一搭建RESTful API
在数字化的浪潮中,后端开发如同一座桥梁,连接着用户界面与数据存储。本文将引导你理解后端开发的核心概念,并通过实践案例,展示如何从零开始构建一个RESTful API。我们将探索设计原则、选择合适的编程语言和框架、数据库交互以及API测试等方面。无论你是初学者还是希望巩固知识的开发者,这篇文章都将为你提供一条清晰的学习路径。
|
5天前
|
存储 JSON JavaScript
探索后端开发:从零构建简易RESTful API
【9月更文挑战第35天】在数字时代的浪潮中,了解如何搭建一个后端服务变得至关重要。本文将通过构建一个简易的RESTful API来揭开后端开发的神秘面纱。我们将使用Node.js和Express框架,逐步引导你理解并实践API的设计、实现与测试过程。无论你是编程新手还是希望扩展技能边界的开发者,这篇文章都将为你提供一次深入浅出的学习旅程。
|
6天前
|
缓存 安全 测试技术
探索后端开发:构建高效API的艺术
【9月更文挑战第34天】在数字世界的幕后,后端开发如同一位默默无闻的艺术家,精心雕琢着每一个数据交互的细节。本文将带你走进后端开发的工作室,揭秘那些让API变得高效、可靠的技术手段。我们将一起学习如何设计RESTful API,使用现代编程语言实现功能,以及确保我们的服务在现实世界中稳定运行的最佳实践。准备好,让我们一起开启这场技术的探索之旅吧!
17 2
|
2月前
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
70 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
2月前
|
小程序 前端开发 JavaScript
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
【避坑宝】是一款企业黑红名单吐槽小程序,旨在帮助打工人群体辨别企业优劣。该平台采用SpringBoot+MybatisPlus+uniapp+uview2等技术栈构建,具备丰富的注释与简洁的代码结构,非常适合实战练习与学习。通过小程序搜索“避坑宝”即可体验。
67 0
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
|
2月前
|
存储 小程序 JavaScript
|
2月前
|
小程序 前端开发 安全
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
45 7
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
64 7