1

I've got a JSON file on AWS s3. I want to make a flutter button to trigger a download of that file from the URL. I've looked around stack quite a bit and I find ways to download JSON generated data but not from a URL. I did think this one was promising:

Force external download url

But I can't seem to get it working in flutter. Here's what I'm doing to try to use anchor with "download" attribute.

First off, my URL looks like this (this is fake though just for the sake of public stack question)

https://myfirmware.s3.amazonaws.com/proxy/version.json

Then I found this answer:

Flutter WEB download option

I tried this but it still (in Chrome Version 80.0.3987.149 (Official Build) (64-bit) shows the JSON in the browser rather than allowing me to save as a file directly on first click of the flutter button.

My flutter code looks like:

                  child: RaisedButton(
                  elevation: 1.0,
                  onPressed: () => 
                     downloadFile("https://myfirmware.s3.amazonaws.com/proxy/version.json"),
                  child: Text("Proxy Config File")),

My import is:

import 'package:universal_html/html.dart' as html;

And that method is:

      void downloadFile(String url){

    html.AnchorElement anchorElement =  new html.AnchorElement();
    anchorElement.href = url;
    anchorElement.download = 'test.json';
    anchorElement.click();

  }

I am not sure how to see how this gets converted when building flutter for the web. But I was hoping this would look a lot like they mentioned here:

Force external download url

Which is:

<a href="http://example.com/media.mp3" download="check this out.mp3">Download Your File</a>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Eradicatore
  • 1,501
  • 2
  • 20
  • 38

1 Answers1

0

You can use flutter_downloader plugin for this use case. Create a task with the download URL and target directory for storage.

final taskId = await FlutterDownloader.enqueue(
  url: downloadUrl,
  savedDir: downloadPath,
);

Make sure to initialize FlutterDownloader before creating a task.

WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize();
Omatt
  • 8,564
  • 2
  • 42
  • 144