1

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');
}
Salim Murshed
  • 1,423
  • 1
  • 8
  • 19
Leo
  • 11
  • 1

1 Answers1

0

I Suggesting You for using SaveFile javascript library same as below source:

https://stackoverflow.com/a/60240531/612124

M.javid
  • 6,387
  • 3
  • 41
  • 56