【问题标题】:Flutter download pdf from flask endpoint responseFlutter 从烧瓶端点响应下载 pdf
【发布时间】:2018-08-02 21:38:52
【问题描述】:

我是 Flutter 的新手,正在尝试接收从后端发送的 PDF

我有一个 python 烧瓶后端,它使用 send_file 使用 get 请求发送 pdf:

return send_file(os.getcwd() + '/output.pdf', attachment_filename='output.pdf')

我正在尝试使用以下方法在颤振中检索它:

File file = new File(_localPath + "output.pdf");

try{
  await http
  .get(PDF_URL + "?property_id=153", 
  headers : {"Content-Type" : "application/json"})
  .then((response) async {
    await file.writeAsBytes(response.bodyBytes);

  });
} catch (Exception) {
  print(Exception.toString());
}

我不知道如何从响应中检索 PDF,因为我有一个使用相同后端的 react Web 应用程序,它使用以下方法成功打开了 PDF:

window.location.assign(response.url, '_blank')

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    避免将await 与then 混用。 await 允许您更连续地编写代码。这种方法将帮助您进行调试,因为您可以查看代码到达的位置、打印响应状态代码等。

    final File file = File('${_localPath}output.pdf');
    try {
      http.Response response = await http.get(
        '$pdfUrl?property_id=153',
        headers: {
          'Content-Type': 'application/json',
        },
      );
      await file.writeAsBytes(response.bodyBytes);
    } catch (e) {
      print(e);
    }
    

    【讨论】:

      【解决方案2】:

      对于那些苦苦挣扎的人,我的问题中的代码有效,它确实从 url 获取 PDF,我只是没有正确访问它。我刚刚在 try catch 块之后添加了这一行,它现在打开了 PDF:

      OpenFile.open("_localPath" + "output.pdf");
      

      也感谢@Richard 的建议!

      【讨论】:

        猜你喜欢
        • 2018-12-09
        • 2021-01-24
        • 1970-01-01
        • 2017-12-04
        • 2021-09-05
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        相关资源
        最近更新 更多