简体   繁体   English

Flutter Dio 包下载路径

[英]Flutter Dio package download path

My main goal is to download a file from a link and then save it to the phone's internal storage so that it'll be accessible through the phone's file manager.我的主要目标是从链接下载文件,然后将其保存到手机的内部存储中,以便可以通过手机的文件管理器访问它。 I'm currently trying out Dio package by running the example code given in the package's repo.我目前正在通过运行包的 repo 中给出的示例代码来试用Dio包。 Upon running the program, I ran into a path problem.运行程序后,我遇到了路径问题。 When I used ./example/flutter.png as the download path, I/flutter (10109): FileSystemException: Cannot open file, path = './example/flutter.png' (OS Error: No such file or directory, errno = 2) shows up.当我使用./example/flutter.png作为下载路径时, I/flutter (10109): FileSystemException: Cannot open file, path = './example/flutter.png' (OS Error: No such file or directory, errno = 2)出现。 And when I used (await getApplicationDocumentsDirectory()).path which produces a String with the value of: /data/user/0/com.example.downloadexample/app_flutter as the path, no error showed up, but the file wasn't there.当我使用(await getApplicationDocumentsDirectory()).path产生一个值为以下的String时: /data/user/0/com.example.downloadexample/app_flutter作为路径,没有出现错误,但文件不是那里。 I tried different variation of the latter path, but with no success.我尝试了后一种路径的不同变体,但没有成功。 Can someone help me with this problem?有人可以帮我解决这个问题吗?

Many thanks in advance.提前谢谢了。

I use http package, not the Dio, the code:我使用http包,而不是Dio,代码:

Future<String> fetchNetFileToDoc(String url, String filename) async {
  final path = await getApplicationDocumentsDirectory();
  File docFile = File('$path/$filename');
    if(await docFile.exists()){
    print( '${docFile.path} exits');
    return docFile.path;
  }

  http.Response response = await http.get(url);
  // todo - check status
  await docFile.writeAsBytes(response.bodyBytes, flush: true);
  return docFile.path;
}

if I invoke this method, like: fetchNetFileToDoc('http://a.com/a.mp3', 'a.mp3') it shows the same error:如果我调用此方法,例如: fetchNetFileToDoc('http://a.com/a.mp3', 'a.mp3')它会显示相同的错误:

FileSystemException: Cannot open file, path= 'Direcotry: '/data/user/0/com.example.master_lu/app_flutter'/a.mp3' (OS Error: No such file or directory, errno = 2)

But if I use getTemporaryDirectory(), change code to this:但是,如果我使用 getTemporaryDirectory(),请将代码更改为:

Future<String> fetchNetFileToTemp(String url, String filename) async {
  Directory tempDir = await getTemporaryDirectory();
  String tempPath = tempDir.path;
  File tempFile = File('$tempPath/$filename');
  if(await tempFile.exists()){
    print( '${tempFile.path} exits');
    return tempFile.path;
  }

  http.Response response = await http.get(url);
  // todo - check status
  await tempFile.writeAsBytes(response.bodyBytes, flush: true);
  return tempFile.path;
}

That's ok, it works with the internal storage.没关系,它适用于内部存储。 Save the file into the data/user/0/com.example.master_lu/cache/a.mp3 The master_lu is the name of my project.将文件保存到data/user/0/com.example.master_lu/cache/a.mp3 ,master_lu是我的项目名称。

The latest=> I sovle my problem, await getApplicationDocumentsDirectory return Future<Directory>最新=> 我解决了我的问题, await getApplicationDocumentsDirectory返回Future<Directory>

so, here is the right code:所以,这是正确的代码:

Future<String> fetchNetFileToDoc(String url, String filename) async {
  final docDir = await getApplicationDocumentsDirectory();
  String docPath = docDir.path;
  File docFile = File('$docPath/$filename');
    if(await docFile.exists()){
    print( '${docFile.path} exits');
    return docFile.path;
  }

  http.Response response = await http.get(url);
  // todo - check status
  await docFile.writeAsBytes(response.bodyBytes, flush: true);
  return docFile.path;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM