提问人:It'sPhil 提问时间:10/20/2023 最后编辑:It'sPhil 更新时间:10/22/2023 访问量:25
如何将 PlatformFile pdf 保存到设备 flutter
How to save PlatformFile pdf to device flutter
问:
我选择了pdf文件作为PlatformFile?selectedPdf文件;我想将其保存回设备,我该怎么做,我正在使用document_file_save_plus但是我的代码中仍然存在错误,有人可以指出问题所在吗
法典
PlatformFile? selectedPdfFile;
void savePdfToDevice() async {
final file = File("${selectedPdfFile?.path}/example.pdf");
Uint8List data = file.readAsBytesSync();
const fileName = "my_sample_file.pdf";
const mimeType = "application/pdf";
setState(() {
DocumentFileSavePlus().saveFile(data, fileName, mimeType);
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('Saved!!')));
});
}
这就是我选择pdf文件的方式
void _openFilePicker() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf'],
);
if (result != null) {
setState(() {
selectedPdfFile = result.files.first;
});
} else {
// User canceled the file picker
}
}
它只是说未处理的异常:FileSystemException:无法打开文件,路径='/data/user/0/com.rateraters.lightscan_pro/cache/writerTempFile6632412770717350316.pdf/example.pdf'(操作系统错误:不是目录,errno = 20) 我不知道这意味着什么
答:
0赞
Chirag Sondagar
10/21/2023
#1
您可以使用此代码将 pdf 保存在设备上
String convertCurrentDateTimeToString() {
return DateFormat('yyyyMMdd_kkmmss').format(DateTime.now());
}
Future<void> _downloadFile(String url) async {
final Dio dio = Dio();
String dirloc = url;
if (Platform.isAndroid) {
dirloc = "/sdcard/download/";
} else {
dirloc = (await getApplicationDocumentsDirectory()).path;
final Directory path = await getApplicationDocumentsDirectory();
dirloc = '${path.path}${Platform.pathSeparator}Download';
final savedDir = Directory(dirloc);
final bool hasExisted = await savedDir.exists();
if (!hasExisted) {
savedDir.create();
}
}
try {
await dio.download(url, "$dirloc${convertCurrentDateTimeToString()}.pdf",
onReceiveProgress: (receivedBytes, totalBytes) {
setState(() {
});
// print('here 2');
},
);
} catch (e) {
// print('catch catch catch');
print(e);
}
setState(() {
context.showMessage("File successfully save.", true);
}
);
}
评论
0赞
It'sPhil
10/22/2023
当我选择pdf时,我将其传递到这个中,但我对你的代码感到困惑。你能尝试实现_downloadFile吗?这将非常有帮助PlatformFile? selectedPdfFile;
String url
PlatformFile? selectedPdfFile;
评论