在 Flutter InAppWebview 中下载文件

Var*_*run 7 download dart flutter flutter-inappwebview

我正在使用 flutter_webview_plugin 包来显示网页。在网页上,我们有一个下载文件的功能。下载文件的逻辑类似于以下代码

 filename = "my-file.txt"
 content = 'any string generated by django'
 response = HttpResponse(content, content_type='text/plain')
 response['Content-Disposition'] = 'attachment;filename={0}'.format(filename) 
 print(response)
 return response
Run Code Online (Sandbox Code Playgroud)

直接在浏览器中调用时工作正常,但在 Flutter Webview 中则没有效果。是否需要手动执行任何操作来处理它?即使我也愿意更改套餐。我也尝试了 flutter_inappwebview 但没有区别。我不想启动任何浏览器的 URL,我只想直接从应用程序下载。可以这样做吗?

Jim*_*hiu 0

我建议您尝试这个包flutter_inappwebview,以获得更好的性能:

  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

...

                      InAppWebView(
                        key: webViewKey,
                        initialUrlRequest:
                        URLRequest(url: Uri.parse("https://inappwebview.dev/")),
                        initialOptions: options,
Run Code Online (Sandbox Code Playgroud)

  • 也尝试使用 flutter-inappwebview ,但没有使用它的结果相同 (2认同)