Download file from url, save to phones storage

后端 未结 4 1779
感动是毒
感动是毒 2020-12-30 06:56

I\'m working on a project, that requires me to download a file from URL, once a button is tapped, and store it to phone storage (probably downloads folder).

Any idea

4条回答
  •  失恋的感觉
    2020-12-30 07:47

    static var httpClient = new HttpClient();
    Future _downloadFile(String url, String filename) async {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();
      var bytes = await consolidateHttpClientResponseBytes(response);
      String dir = (await getApplicationDocumentsDirectory()).path;
      File file = new File('$dir/$filename');
      await file.writeAsBytes(bytes);
      return file;
    }
    

提交回复
热议问题