Flutter url_launcher 下载pdf文件而不是在浏览器中打开它

发布于 2025-01-21 00:06:50 字数 626 浏览 4 评论 0原文

我正在使用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";
                                                    },

I'm using url_launcher: ^5.7.5 and when i'm passing a pdf url in the launch function it it keeps downloading the pdf instead of opening it on my browser,

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";
                                                    },

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2025-01-28 00:06:50

在新版本的 URl_laucher 中,您需要手动指定启动模式,如下所示:

return await launchUrl(Uri.parse(command),
        mode: LaunchMode.externalNonBrowserApplication);

for webview mode: Launch.inAppWebView;

In the new version of URl_laucher you need to manually specify the launch mode like this:

return await launchUrl(Uri.parse(command),
        mode: LaunchMode.externalNonBrowserApplication);

for webview mode: Launch.inAppWebView;
才能让你更想念 2025-01-28 00:06:50

更新了url_launcher flutter软件包的较新版本。 启动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);
}

Updated for the newer versions of the url_launcher flutter package. The launch and canLaunch functions are deprecated.

You can simplify your code and extract it in a compact function:

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);
}
撩动你心 2025-01-28 00:06:50

我相信在这种情况下,最好使用 webview_flutter: ^3.0.2 而不是 laucher ,希望对您有帮助!

I believe that in this case, would be better you be using webview_flutter: ^3.0.2 instead of laucher, hope that helps you!

咿呀咿呀哟 2025-01-28 00:06:50

我尝试了所有方法,但对我有用的是,不要直接启动文件 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'

I tried everything but what worked for me was instead of launching your file URL directly, attach Google Drive embed link to it

url = 'https://docs.google.com/gview?embedded=true&url=${fileUrl}'

For your case fileUrl = 'http://3.65.45.149/uploads/store/vendor_report/vendor_pickup_report_257.pdf'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文