【发布时间】:2021-10-09 08:12:12
【问题描述】:
我使用 firebase 云存储来存储一些文件,然后,我必须将它们放在一个 zip 文件中并下载它,我必须在网络上执行此操作仅,这就是困难的部分。 我现在是这样做的:
FloatingActionButton(
child: const Icon(Icons.download),
onPressed: () async {
List<int>? bytes;
webarc.ZipEncoder encoder = webarc.ZipEncoder();
webarc.Archive archive = webarc.Archive();
try {
List<String> links = await Database.formLinks();
for (String link in links) {
String fileName = link.substring(link.lastIndexOf('/') + 1);
http.Response response = await http.get(
Uri.parse(link),
headers: headers,
);
webarc.ArchiveFile file = webarc.ArchiveFile.stream(
fileName,
response.bodyBytes.elementSizeInBytes,
response.bodyBytes.toList(),
);
archive.addFile(file);
}
webarc.OutputStream outputStream = webarc.OutputStream(
byteOrder: webarc.LITTLE_ENDIAN,
);
bytes = encoder.encode(
archive,
level: webarc.Deflate.BEST_COMPRESSION,
modified: DateTime.now(),
output: outputStream,
);
} catch (e) {
print(e);
Fluttertoast.showToast(
msg: 'Errore nel download dello zip.',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
return;
}
if (bytes == null) {
Fluttertoast.showToast(
msg: 'Errore nel download dello zip. bytes nulli.',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
return;
}
String zipFileName = 'forms.zip';
await FileSaver.instance.saveFile(
zipFileName,
Uint8List.fromList(bytes),
'zip',
mimeType: MimeType.ZIP,
);
Fluttertoast.showToast(
msg: 'Zip downloadato correttamente.',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
},
),
一切都很完美,除了我下载了 zip 文件,但 zip 文件是空的。
我尝试了很多不同的方法,用 html 包下载它,锚定并单击一个 div,但现在我认为该包不起作用,我该怎么做?任何帮助都将被接受,谢谢。
附: 这是 database.formLinks 函数:
static Future<List<Reference>> getCompiledForms() async =>
(await formsRef.listAll()).items;
static Future<List<String>> formLinks() async {
final List<Reference> forms = await getCompiledForms();
final List<String> links = [];
for (final form in forms) {
String url = await form.getDownloadURL();
links.add(url);
}
return links;
}
这些是我在这些功能中使用的包:
存档:^3.1.2(作为 webarc)
http: ^0.13.3(作为 http)
file_saver:^0.0.10
fluttertoast:^8.0.7
cloud_firestore:^2.3.0
【问题讨论】:
标签: flutter web get zip firebase-storage