我正在对 Flutter 移动应用程序进行 Web 化,并尝试解决 Web 中对路径提供程序缺乏支持的问题。
我正在使用 PDF (pdf: ^1.9.0) 包生成 PDF 文档并将其上传到 Google Drive,我试图找出是否可以生成 PDF 并将其存储在内存中以使其与网络兼容。
使用路径提供程序的当前代码示例。
createFile() async {
final downloads = await getApplicationSupportDirectory();
final file = File("${downloads.path}/$filename.pdf");
await file.writeAsBytes(pdf.save());
await GoogleDriveController.uploadFileToGoogleDrive(file.path);
}
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法使用 Flutter web 为 web 生成并存储 Fies 在内存中?
Yon*_*kee 10
我设法找到一种解决方法来生成 PDF 并通过浏览器触发下载,并认为我应该发布以防有人偶然发现这一点。
//Create PDF in Bytes
Uint8List pdfInBytes = pdf.save();
//Create blob and link from bytes
final blob = html.Blob([pdfInBytes], 'application/pdf');
final url = html.Url.createObjectUrlFromBlob(blob);
final anchor = html.document.createElement('a') as html.AnchorElement
..href = url
..style.display = 'none'
..download = 'pdf.pdf';
html.document.body.children.add(anchor);
//Trigger the download of this PDF in the browser.
RaisedButton(
child: Text('Press'),
onPressed: () {
anchor.click();
Navigator.pop(context);
},
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8223 次 |
| 最近记录: |