0

I just want to download an image from the URL instead of open it in a new tab. I already try some code, but it didn't work as I want

1. url_launcher

launch(my_url);

2. url_launcher_web

final launcher = UrlLauncherPlugin();
launcher.launch(my_url);

3. dio

final response = await Dio().download(my_url, './xx.html');

UPDATE

Please note that I don't want to open the file in a new tab or current tab, I just want to direct download the file (something like making a stream to download).

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30
  • 1
    Does this answer your question? [flutter download an Image from url](https://stackoverflow.com/questions/52299112/flutter-download-an-image-from-url) – Mayur Agarwal Sep 24 '21 at 04:26
  • I know how to download file from mobile, I need to direct download file from a web – NM Naufaldo Sep 24 '21 at 04:27

1 Answers1

0

For flutter web to download any file you can take the help of HTML library which will treat it as simply an anchor tag with download option. Note this can download any file so better check for IMAGE URL'S as well.

import 'dart:html' as html;

void downloadImage(String url){
   html.AnchorElement anchorElement =  new html.AnchorElement(href: url);
   anchorElement.download = url;
   anchorElement.click();
}
Krish Bhanushali
  • 1,594
  • 1
  • 8
  • 16
  • Before you give me this answer, I already try this code from another StackOverflow answer and it didn't download the file directly. I have to right-click and `Save image as...` – NM Naufaldo Sep 24 '21 at 07:28
  • Hi @Kris. It's a nice solution, but it does not allow compiling on Android and IOS. It's not a cross-platform solution. Any ideas to fix that issue? – David L Dec 02 '21 at 14:01