🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / Dio ile pdf indirme ve OpenFile ile görüntüleme download

1-) FLUTTER - Dio ile pdf indirme ve OpenFile ile görüntüleme download

 

**** BİRİNCİ YOL FlutterDownloader ile ***

 

terminale yaz -> flutter pub add flutter_downloader

 

İNİTİALİZİNG

 

 void callback(String id ,DownloadTaskStatus status,int progress){}

 

Future<void> main() async {
  
WidgetsFlutterBinding.ensureInitialized();
  
await FlutterDownloader.initialize(debug: true, ignoreSsl: true); // , ignoreSsl: true
  
FlutterDownloader.registerCallback(callback);

  runApp(const MyApp());}

 

 

PATH İÇİN

 

Future<String?> getDownloadPath() async {

    Directory? directory;

    try {

      if (Platform.isIOS) {

        directory = await getApplicationDocumentsDirectory();

    } else {

        directory = Directory('/storage/emulated/0/Download');

        // Put file in global download folder, if for an unknown reason it didn't exist, we fallback

        // ignore: avoid_slow_async_io

        if (!await directory.exists()) directory = await getExternalStorageDirectory();

}

    } catch (err, stack) {

    print("Cannot get download folder path");

}

return directory?.path;

}

 

DOSYA İNDİRME

 

// String? dosyaYolu = await getDownloadPath();

String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";

 await FlutterDownloader.enqueue(
               url:
urlim ,
        showNotification:
true,
        openFileFromNotification:
true,

        saveInPublicStorage: true,

        savedDir: (await getExternalStorageDirectory())!.path);

 

 

 

 

**** İKİNCİ YOL CHROME YÖNLENDİRİR ***

 

String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";

final String _url_files = "$urlim";
void _launchURL_files() async =>
    
await canLaunch(_url_files) ? await launch(_url_files) : throw 'Could not launch $_url_files';
_launchURL_files();

 

 

1. MobileDownloadService.dart

 

 

import 'package:dio/dio.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class MobileDownloadService  {
  
@override
  
Future<void> download({required String url}) async {
    
bool hasPermission = await _requestWritePermission();
    
if (!hasPermission) return;

    
Dio dio = Dio();
    
var dir = await getApplicationDocumentsDirectory();

    
// You should put the name you want for the file here.
    // Take in account the extension.
    
String fileName = 'myFile.pdf';
    
await dio.download(url, "${dir.path}/$fileName");
    
OpenFile.open("${dir.path}/$fileName", type: 'application/pdf');
  }

  
Future<bool> _requestWritePermission() async {
    
await Permission.storage.request();
    
return await Permission.storage.request().isGranted;
  }
}

 

2. KULLANIMI

 

 

String urlim = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";

MobileDownloadService downloadService =
    
new MobileDownloadService();
downloadService
    
.download(url: urlim)
    .then((value) {});

 

 

kaynak : https://github.com/MCarlomagno/flutter_download_button

 

 

4. InAppWebView ile

 

onDownloadStart: (controller,url,) async {  
    
await FlutterDownloader.enqueue(
               url: url.toString(),
        showNotification:
true,
        openFileFromNotification:
true,
        savedDir: "/storage/emulated/0/Download"   );},

 

 

5. AndroidManifest.xml

 

<uses-permission android:name="android.permission.INTERNET"/>

<
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<
uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

 

 

 <provider
        
android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
        
android:authorities="${applicationId}.flutter_downloader.provider"
        
android:exported="false"
        
android:grantUriPermissions="true">
        <
meta-data
            
android:name="android.support.FILE_PROVIDER_PATHS"
            
android:resource="@xml/provider_paths"/>
    </
provider>

 

<provider
    
android:name="com.pichillilorenzo.flutter_inappwebview.InAppWebViewFileProvider"
    
android:authorities="${applicationId}.flutter_inappwebview.fileprovider"
    
android:exported="false"
    
android:grantUriPermissions="true">
    <
meta-data
        
android:name="android.support.FILE_PROVIDER_PATHS"
        
android:resource="@xml/provider_paths" />
</
provider>


</
application>

 

6. Android->app->src->main->res->xml->provider_paths.xml

 

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <
external-path
        
name="external_files"
        
path="." />
    <
external-files-path
        
name="external_files"
        
path="." />
    <
cache-path
        
name="cache"
        
path="." />
    <
external-cache-path
        
name="external_cache"
        
path="." />
    <
files-path
        
name="files"
        
path="." />


</
paths>

 

 

 

SON NOT ANDROİD İÇİN BUNU KULLAN savedDir: (await getExternalStorageDirectory())!.path);

İOS İÇİN BUNU KULLAN directory = await getApplicationDocumentsDirectory();

 

 

 2022 Ekim 22 Cumartesi
 553