0

I am developing a web app with Flutter (web) and I need to download a file (of any type of extension) via a URL, DOWNLOAD it and not open it through the browser. The problem is that using this code, it does download the file but it seems to be corrupted.

String downloadFile(String filename) {

    String? url= 'https://www.xxxxxx.it/yyyy/zzzz'+'/'+filename;
    
        var ciao = html.Url.createObjectUrlFromBlob(html.Blob([url]));
        html.AnchorElement(href: ciao)
          ..setAttribute('download', filename)
          ..click();
    
    return "File "+filename+" in download!";

  }

enter image description here

enter image description here

enter image description here

1 Answer 1

0

It works now:

String downloadFile(String fileName, String URL){
     String? url= URL+'/'+  fileName;
     final anchor = html.AnchorElement(href: url)
       ..setAttribute('download', fileName)
       ..setAttribute('target', '_blank')
       ..click();
     return "File "+fileName+" scaricato in download!";
   }

Explanation:

Target attribute: Setting the target attribute to _blank helps ensure that the file is handled as a download.

Download attribute: Setting the download attribute with the filename forces the download.

Not the answer you're looking for? Browse other questions tagged or ask your own question.