js 读取word和txt(react版) + 正则分割段落
show the code
前提:需要mammoth包~
import React, { useState, useReducer } from 'react';
import { Button, Alert, Table, Badge, Input, Upload } from 'antd';
import CommonTable from '@cps/CommonTable';
import styles from './receive.less';
import { UploadOutlined } from '@ant-design/icons';
import mammoth from 'mammoth';
function reducer(state: any, action: any) {
return {
...state,
...action.data
};
}
const NovelAnalyze = () => {
const [state, dispatch] = useReducer(reducer, {
content: ''
});
const getTextInfo = (file: any) => {
const reader = new FileReader();
reader.readAsText(file, 'gb2312');
reader.onload = (result: any) => {
console.log(result);
let targetNum = result.target.result;
console.log(targetNum);
};
return false;
};
const getWordInfo = (file: any) => {
const reader = new FileReader();
reader.onload = function(loadEvent: any) {
const arrayBuffer = loadEvent.target['result'];
mammoth
.extractRawText({ arrayBuffer: arrayBuffer })
.then(function(resultObject) {
console.log('extractRawText', resultObject.value);
})
.done();
};
reader.readAsArrayBuffer(file);
return false;
};
return (
<div>
<ul className={styles.optionBtnList}>
<li>
<Button type='primary'>导出</Button>
<Upload action='' accept='text/plain' beforeUpload={getTextInfo} showUploadList={false}>
<Button>
<UploadOutlined />
上传txt文件
</Button>
</Upload>
<Upload
action=''
accept='.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
beforeUpload={getWordInfo}
showUploadList={false}
>
<Button>
<UploadOutlined />
上传word文件
</Button>
</Upload>
<Button type='primary'>导入word</Button>
</li>
</ul>
</div>
);
};
export default NovelAnalyze;
正则分割段落
原文:
第259章 这是259内容 第262章 这是262内容 第666章 测试内容
str.replace(/\s*(第\d+章)\s*/g, "@@@$1___").split("@@@").filter(item => item).map(item => ({[item.split("___")[0]]: item.split("___")[1]}))
结果:
0: {第259章: "这是259内容"}
1: {第262章: "这是262内容"}
2: {第666章: "测试内容"}
js 读取word和txt(react版) + 正则分割段落的更多相关文章
- js读取修改创建txt文本类型文件(.ini)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js读取本地json/txt/xml存在跨越问题,可以用jsonp 读取本地json文件
想自己用 js写一个原生的ajax请求,访问本地文件,json/txt.但是demo,写了一个后,发现 原来是跨域了. js 写的原生ajax 请求代码如下 html代码 <div id=&qu ...
- js读取文本内容,支持csv.txt
js读取文本内容,支持csv.txt <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- ZK中使用JS读取客户端txt文件内容问题
最近写一个需求时遇到一个问题,用户需要通过点击一个按钮直接读取他自己电脑上D盘的一个txt文件内容显示到页面,因为项目现在是用ZK写的.我对于ZK也是刚刚了解不就,很多都还不是很熟.起初我是想用io流 ...
- POI 读取word (word 2003 和 word 2007) (转)
最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...
- php 如何写入、读取word,excel文档
如何在php写入.读取word文档 <? //如何在php写入.读取word文档 // 建立一个指向新COM组件的索引 $word = new COM("word.applicatio ...
- word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得
word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得本人第一次写博客,有上面不足的或者需要改正的希望大家指出来,一起学习交流讨论.由于在项目中遇到了这一个问题,在 ...
- 使用新一代js模板引擎NornJ提升React.js开发体验
当前的前端世界中有很多著名的开源javascript模板引擎如Handlebars.Nunjucks.EJS等等,相信很多人对它们都并不陌生. js模板引擎的现状 通常来讲,这些js模板引擎项目都有一 ...
- C# 添加、读取Word脚注尾注
脚注和尾注是对文本的补充说明.脚注一般位于页面的底部,可以作为文档某处内容的注释:尾注一般位于文档的末尾,列出引文 的出处等.在本示例中将介绍如何来添加或删除Word脚注. 工具使用:Free Spi ...
随机推荐
- POJ 3057 Evacuation 题解
题目 Fires can be disastrous, especially when a fire breaks out in a room that is completely filled wi ...
- Vue中computed的本质及与methods的区别
一.computed的本质? computed为什么不像methods一样加小括号使用? 正常使用computed方式 运行结果 至于为什么computed为什么不像methods一样使用小括号调用, ...
- springboot freemark linux 找不到ftl文件
文件路径: 再idea中这样写是可以正常导出文件,但是 打成jar放到linux上就找不到文件了. String templatePath =WordUtils.class.getResource(& ...
- 解决for循环里获取到的索引是最后一个的问题
方法一 原理: 利用 setTimeout 函数的第三个参数,会作为回调函数的第一个参数传入 利用 bind 函数部分执行的特性 代码 1: for (var i = 0; i < 10; i+ ...
- 数据可视化之powerBI技巧(十六)采悟:PowerBI作图技巧:动态显示可视化标题
默认情况下,PowerBI图表的标题是静态的,为了增强图表的可读性,通过设置动态标题,可快速展示关键信息.提升沟通效率.本文通过两个简单的例子来看看PowerBI中如何创建动态标题. /01/ 拿之前 ...
- Log4net控制台和窗体程序无法输出记录的原因之一
在asp.net web应用程序中,读取log4net的配置文件可以这样写: [assembly:log4net.Config.XmlConfigurator(ConfigFile="Web ...
- mysql常见数据类型
#常见的数据类型 /* 数值型: 整型 小数: 定点数 浮点数 字符型: 较短的文本:char.varchar 较长的文本:text.blob(较长的二进制数据) 日期型: */ #一.整型 /* 分 ...
- 定时器三----js定时器
方法一: var t; //初始化定时器 $(function(){ init_fun_timer1(); }); ...
- Burp Suite Spider Module - 网络爬虫模块
Web application spdiering 和scanning 可以结合使用. Burp Suite 的Spider Module - Options 主要包含:Crawler Setting ...
- JavaScript动画实例:粒子文本
1.粒子文本的实现原理 粒子文本的实现原理是:使用两张 canvas,一张是用户看不到的canvas1,用来绘制文本:另一张是用户看到的canvas2,用来根据canvas1中绘制的文本数据来生成粒子 ...