44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
// 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);
|
||
}
|
||
});
|
||
}
|
||
}) |