【发布时间】:2021-05-25 07:13:58
【问题描述】:
我有一个本地资产、一个 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,
),
);
}
}
【问题讨论】:
-
您是否在资产中添加了您的
.docx文件并在pubspec.yaml中添加了一个文件路径,例如assets: – assets/myfile.docx? -
是的,我只是尝试按 1:1 复制您的代码以查看它是否有效,但不,请在评论下方查看我的答案 我不明白为什么,使用 .txt 可以像一个魅力,但不是 docx。
-
WebViewX是否在 webview_flutter 包内? -
我不知道,但你的代码运行良好,我已经复制了它并且运行顺利。
-
好吧,就这样吧。删除您的 .docx 文件并创建一个新文件。而不是通过打开word来添加内容意味着不要打开MS word来添加内容。使用记事本或崇高文本在文件中添加内容并复制粘贴您的 HTML 代码即可。像这样试试。但它的加载数据但没有显示在
WebViewX
标签: flutter file dart docx readfile