Reputation: 2861
i make a flutter application, first i display all information about pdf file, and it is include the url of the file, comming form Restfull API, everything work good, but i want to download this file when user click on it, i try this:
onTap: () async {
// downloadFile(snapshot.data[index].file);
Dio dio = new Dio();
Directory dir =
await getApplicationDocumentsDirectory();
String path = dir.path;
await dio.download(
snapshot.data[index].file, path);
},
snapshot.data[index].file, path is the right path or file that come from api, but it is not work, and give me an error with (path) in this code
await getApplicationDocumentsDirectory();
String path = dir.path;
this is my error log:
E/flutter (26482): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter (26482): FileSystemException: Cannot open file, path = '/data/data/com.example.mis/app_flutter' (OS Error: Is a
directory, errno = 21)
E/flutter (26482): #0 _File.throwIfError (dart:io/file_impl.dart:647:7)
E/flutter (26482): #1 _File.openSync (dart:io/file_impl.dart:491:5)
E/flutter (26482): #2 Dio.download (package:dio/src/dio.dart:212:20)
E/flutter (26482): <asynchronous suspension>
E/flutter (26482): #3 MaterialsState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
(package:mis/courses/materials.dart:121:47)
E/flutter (26482): <asynchronous suspension>
E/flutter (26482): #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter (26482): #5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter (26482): #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (26482): #7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (26482): #8 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (26482): #9 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (26482): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent
(package:flutter/src/gestures/binding.dart:184:20)
E/flutter (26482): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent
(package:flutter/src/gestures/binding.dart:158:22)
E/flutter (26482): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent
(package:flutter/src/gestures/binding.dart:138:7)
E/flutter (26482): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue
(package:flutter/src/gestures/binding.dart:101:7)
E/flutter (26482): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket
(package:flutter/src/gestures/binding.dart:85:7)
E/flutter (26482): #15 _invoke1 (dart:ui/hooks.dart:159:13)
E/flutter (26482): #16 _dispatchPointerDataPacket (dart:ui/hooks.dart:113:5)
is there any other method to download file? or even to just open it? thank you very much
Upvotes: 3
Views: 12643
Reputation: 1837
You can use this package flutter cache manager
using this code to get your file:
String url = 'https://fileUrl';
File file = await DefaultCacheManager().getSingleFile(url);
then you can use 'file' to do what you want.
Upvotes: 0
Reputation: 21
Use plugin File support on flutter https://pub.dev/packages/file_support
Future<File> downloadFile async{
File file =await FileSupport(url:"http://test.com/.mp4");
return file;
}
Upvotes: 1
Reputation: 9581
Just in case anyone else needs this.
The problem is that you didn't pass a file path as parameter but passed a directory.
await dio.download(snapshot.data[index].file, path+'/fileName.pdf');
That should solve the issue.
Upvotes: 7