javascript - Flutter Web 下载文件在移动浏览器中不起作用
问题描述
我正在创建颤振网络,用于下载图像。下载或保存功能仅在 Desktop 中提供。移动浏览器无法下载或保存图像。
这是我的代码:
未来 getWidgetImage() 异步 {
try {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
debugPrint(bs64.length.toString());
print("convert done");
imageList.add(pngBytes);
if (kIsWeb) {
websaveAndLaunchFile(bytes, qrName+'.png');
}
else {
mobilesaveAndLaunchFile(bytes, qrName+'.png');
}
return pngBytes;
} catch (exception) {
return null;
}
}
//web保存包
Future <void> websaveAndLaunchFile (List<int> bytes,String fileName)async {
AnchorElement(
href: "data:application/octet-
stream;charset=ut16le;base64,${base64.encode(bytes)}")
..setAttribute("Download", fileName)
..click();
}
//mobileSave包
Future<void> mobilesaveAndLaunchFile(List<int> bytes, String fileName)async{
final path = ( await getExternalStorageDirectory()).path;
final file = File('$path/$fileName');
await file.writeAsBytes(bytes, flush: true);
OpenFile.open('$path/$fileName');
}
解决方案
我建议您使用SaveFile
与以下来源相同的 javascript 库:
推荐阅读
- swift - UIScrollView在旋转视图Swift时开始缩放,如何禁用缩放?
- karate - 空手道司机同步挑战
- spring - 如何在 Spring Boot 中配置两个数据库?
- c# - 将 JSON 发送到 C# 中的 GET 方法
- powerbi - 查找具有相同 ID 的最后一小时值 (Power BI)
- java - Spring Repository 集成测试
- android - Android - 检测可以创建多大的位图而不会崩溃
- model-view-controller - Xero oauth 1.0 转换为 2.0
- php - 如何将 PDO 请求的结果放入 JSON 文件中
- random - SPSS 从受访者的输入中随机选择一个数字