Reading Local Files in UniApp

In uniapp, you can use the uni.readFile method provided by uniapp to read local files. The syntax of this method is as follows:

uni.readFile({
  filePath: '', // 文件路径,支持本地路径、临时路径和保存文件路径
  encoding: '', // 指定读取文件的字符编码,不传时以 ArrayBuffer 格式读取文件内容
  success: (res) => {
    // 读取文件成功时的回调函数
    console.log(res.data); // 文件内容,如果在读取时指定了 encoding 参数,则返回字符串,否则返回 ArrayBuffer
  },
  fail: (err) => {
    // 读取文件失败时的回调函数
    console.log(err);
  }
});

It is important to note that the value of the filePath parameter can be a local file path, a temporary file path, or a saved file path.

bannerAds