织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

当前位置: 欧博ABG-会员注册-官网网址 > abg欧博 > 文章页

欧博abgHow to download pdf to mobile device flutter

时间:2025-09-25 15:06来源: 作者:admin 点击: 22 次
There are a few things to consider. Saving a file differs on various devices. In the case of PDF files, I assume that you want them to be easily acces

There are a few things to consider.

Saving a file differs on various devices. In the case of PDF files, I assume that you want them to be easily accessible to the user. To make it happen, it can't be stored in the application's files - it would require a lot of digging to find the file by the user himself.

It also requires several permissions, some of which are configured directly in platform-specific files and some have to be requested from the user in runtime. Permission Handler package comes in handy for this job.

So, here is how to implement it:

import 'dart:io'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; import 'package:permission_handler/permission_handler.dart';

iOS

Future<void> saveFile(String fileName) async { var file = File(''); // Platform.isIOS comes from dart:io if (Platform.isIOS) { final dir = await getApplicationDocumentsDirectory(); file = File('${dir.path}/$fileName'); }

To make it work on iOS you have to add 2 keys to ios/Runner/Info.plist file.

<key>LSSupportsOpeningDocumentsInPlace</key> <true/> <key>UIFileSharingEnabled</key> <true/>

Android

if (Platform.isAndroid) { var status = await Permission.storage.status; if (status != PermissionStatus.granted) { status = await Permission.storage.request(); } if (status.isGranted) { const downloadsFolderPath = '/storage/emulated/0/Download/'; Directory dir = Directory(downloadsFolderPath); file = File('${dir.path}/$fileName'); } }

For Android, on the other hand, you'll have to do a few more things.

At first, it is required to set proper compileSdkVersion in android/app/build.gradle file. As I'm writing this - it should be set to 33.

Also 2 lines are needed in android/app/src/main/AndroidManifest.xml, right below manifest

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

And of course, as it can be seen in the code - we also need to request a permission from a user.

Writing to the file

// load file that you want to save on user's phone // it can be loaded from whenever you want, e.g. some API. final byteData = await rootBundle.load('assets/$fileName'); try { await file.writeAsBytes(byteData.buffer .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)); } on FileSystemException catch (err) { // handle error } }

File Saver package saves files to the application's data, so it won't be so easy to find for the user. It is also published by an unverified uploader so I'd definitely keep it in mind.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-10-14 04:10 最后登录:2025-10-14 04:10
栏目列表
推荐内容