0

I am trying to download file using Dio and pathProvider but it is getting failed i tried This Soln however the print progress is showing that it is dowloaded 100% and No error is thrown but i could not find the downloaded file i am using Android 13 Api 33

void onDownloadClick(String url) async {
    String fileName = url.split("/").last;
    var tempDir = await getTemporaryDirectory();
    String fullPath =tempDir.path.toString() +"/" + fileName;
    debugPrint('full path ${fullPath}');
    var dio = Dio();
    download2(dio, url, fullPath);
  }

  Future download2(Dio dio, String url, String savePath) async {
    try {
      Response response = await dio.get(
        url,
        onReceiveProgress: showDownloadProgress,
        //Received data with List<int>
        options: Options(
            responseType: ResponseType.bytes,
            followRedirects: false,
            validateStatus: (status) {
              return status! < 500;
            },),
      );
      debugPrint(response.headers.toString());
      File file = File(savePath);
      var raf = file.openSync(mode: FileMode.write);
      // response.data is List<int> type
      raf.writeFromSync(response.data);
      await raf.close();

    } catch (e) {
      debugPrint("Got Error  ${e.toString()}");
    }
  }

  void showDownloadProgress(received, total) {
    if (total != -1) {
      debugPrint((received / total * 100).toStringAsFixed(0) + "%");
    }
  }

Thanks

1 Answers1

1

You can download your file and save it in your android download folder for that you can follow below code.

Directory? directory;
try {
  if (Platform.isIOS) {
    directory = await getApplicationDocumentsDirectory();
  } else {
    directory = await getExternalStorageDirectory();
    String newPath = directory?.path.substring(0, 20) ?? "";
    newPath += "Download";
    directory = Directory(newPath);
    if(!directory.existsSync()){
      await directory.create();
    }
  }
} catch (err) {
  print("download folder not exist");
}
Gazi Mohib
  • 71
  • 8