I'm create flutter web, for downloading image. Download or save function is only function in Desktop. Mobile Browser can not download or save image.
Here is my code:
Future getWidgetImage() async {
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;
}
}
//websave package
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 Package
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');
}