我有一个本地资源,一个Docx文件(或者在某些情况下只有UInt8List字节),如何从这个文件(或从字节)读取数据?
这个数据只包含字符串,我怎么能读它?我可以读.txt文件,但没有docx,为什么?事实上它没有特殊字符,所以问题不是编码。(检查下面的内容,HtmlCode)
我试过了:这是字节
final data = await rootBundle.load('template.docx');
final bytes = data.buffer.asUint8List();
这是一个()的字符串
String fileText = await rootBundle.loadString('template.docx');
错误:
Error: FormatException: Unexpected extension byte (at offset 16)
at Object.throw_ [as throw] (http://localhost:54436/dart_sdk.js:5366:11)
at convert._Utf8Decoder.new.convertGeneral (http://localhost:54436/dart_sdk.js:49632:19)
at convert._Utf8Decoder.new.convertSingle (http://localhost:54436/dart_sdk.js:49604:19)
at Utf8Decoder.convert (http://localhost:54436/dart_sdk.js:49463:67)
at Utf8Codec.decode (http://localhost:54436/dart_sdk.js:49172:22)
at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:54436/packages/flutter/src/services/system_channels.dart.lib.js:2289:31)
at loadString.next (<anonymous>)
at http://localhost:54436/dart_sdk.js:39272:33
at _RootZone.runUnary (http://localhost:54436/dart_sdk.js:39129:58)
at _FutureListener.thenAwait.handleValue (http://localhost:54436/dart_sdk.js:34091:29)
at handleValueCallback (http://localhost:54436/dart_sdk.js:34651:49)
at Function._propagateToListeners (http://localhost:54436/dart_sdk.js:34689:17)
at _Future.new.[_completeWithValue] (http://localhost:54436/dart_sdk.js:34531:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:54436/dart_sdk.js:34554:35)
at Object._microtaskLoop (http://localhost:54436/dart_sdk.js:39416:13)
at _startMicrotaskLoop (http://localhost:54436/dart_sdk.js:39422:13)
at http://localhost:54436/dart_sdk.js:34905:9
字节没有任何问题,但是字符串会抛出这个错误,我该怎么解决呢?
完整代码:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Html(),
);
}
}
class Html extends StatefulWidget {
LoadHtml createState() => LoadHtml();
}
class LoadHtml extends State<Html> {
void initState() {
readFilesFromAssets();
super.initState();
}
// Read .docx function not only .docx mostly any type .json .txt etc
readFilesFromAssets() async {
print("read now");
String assetContent = await rootBundle.loadString('assets/template.docx');
print("assetContent : $assetContent");
}
//this is exaclty what would be in the template.doc
var HtmlCode = r"""
<HTML>
<BODY LANG="ru-RU" DIR="LTR">
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Example
Document</SPAN></FONT></FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"> <FONT FACE="Times New Roman, serif"><FONT SIZE=5><SPAN LANG="en-US"><B>Document
name</B></SPAN></FONT></FONT></P>
<P LANG="en-US" CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in">
<BR>
</P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Szerzodes
tobbi resze:</SPAN></FONT></FONT></P>
</BODY>
</HTML>
""";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('From docx')),
body: WebViewX(
initialContent: HtmlCode, //I should read this from the .docx
initialSourceType: SourceType.HTML,
),
);
}
}
2条答案
按热度按时间63lcw9qa1#
首先将您的
.docx
文件添加到assets
文件夹中,与lib
文件夹相同级别。然后像这样将
assets
文件夹添加到pubspec.yaml
在你的
main.dart
里面添加这个函数后,即initState()
里面的readFileFromAssets()
,当应用程序启动时,它将开始从.docx
加载数据。并导入此包太
import 'package:flutter/services.dart';
下面是输出:
yhuiod9q2#
您可以使用Docs to text packages
这是一个小软件包,允许您从.docx文件(Microsoft Word)中检索文本(仅文本)。
感谢SnowLukin