我正在使用 url_launcher: ^5.7.5,当我在启动函数中传递 pdf url 时,它会继续下载 pdf,而不是在浏览器上打开它,
onTap: () async {
url="http://3.65.45.149/uploads/store/vendor_report/vendor_pickup_report_257.pdf";
if (await canLaunch(url)){
await launch(url,
headers: { "Content-Type":"application/pdf",
"Content-Disposition":"inline"}, );
print("browser url");
print(url);
}
else
// can't launch url, there is some error
throw "Could not launch $url";
},
在新版本的
URl_laucher
中,您需要手动指定启动模式,如下所示:
return await launchUrl(Uri.parse(command),
mode: LaunchMode.externalNonBrowserApplication);
for webview mode: Launch.inAppWebView;
针对较新版本的
url_launcher
flutter 包进行了更新。 launch
和 canLaunch
函数已弃用。
您可以简化代码并将其提取到一个紧凑的函数中:
void openPdfFromUrl(String url) {
debugPrint('opening PDF url = $url');
var googleDocsUrl = 'https://docs.google.com/gview?embedded=true&url=${Uri.encodeQueryComponent(url)}';
debugPrint('opening Google docs with PDF url = $googleDocsUrl');
final Uri uri = Uri.parse(googleDocsUrl);
launchUrl(uri);
}
我相信在这种情况下,你最好使用 webview_flutter: ^3.0.2 而不是laucher,希望对你有帮助!
我尝试了所有方法,但对我有用的是不要直接启动文件 URL,而是附加 Google Drive 嵌入链接
url = 'https://docs.google.com/gview?embedded=true&url=${fileUrl}'
对于您的情况 fileUrl = 'http://3.65.45.149/uploads/store/vendor_report/vendor_pickup_report_257.pdf'
如何打开需要授权标头的PDF URL?