ts_aimz/pages/readTxt/readTxt.js
2025-04-03 10:44:12 +08:00

44 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/readTxt/readTxt.js
Page({
/**
* 页面的初始数据
*/
data: {
fileUrl: '',
txtParagraphs:[]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
wx.setNavigationBarTitle({
title: '文件预览',
})
this.setData({
fileUrl: options.filePath
})
this.readTxt()
},
readTxt() {
const _self= this
const fs = wx.getFileSystemManager();
fs.readFile({
filePath: _self.data.fileUrl,
encoding: 'utf8',
success: (readRes) => {
const fileContent = readRes.data;
// 简单示例:按空行拆分段落
const paragraphs = fileContent.split('\n\n').map((p) => p.trim()).filter((p) => p);
// 将处理后的段落数据设置到页面数据中
this.setData({
txtParagraphs: paragraphs
});
},
fail: (readErr) => {
console.error('读取txt文件内容失败', readErr);
}
});
}
})