【问题标题】:Issue with serving files with fastAPI使用 fastAPI 提供文件的问题
【发布时间】:2021-04-23 07:55:23
【问题描述】:

我正在尝试从 SFTP 服务器获取字节文件并将其作为文件对象传递给 fastAPI 到服务器。

        flo = BytesIO()
        srv.getfo(file, flo)
        kind = mimetypes.guess_type(file)
        flo.seek(0)
        return FileResponse(flo.read(), media_type=kind[0])

我收到此错误

AssertionError: 'aiofiles' must be installed to use FileResponse

所以我尝试了流式响应并得到一个空的下载文件。

return StreamingResponse(flo.read(), media_type=kind[0])

这是我的前端,只是使用了一个简单的 axios 获取和文件下载库

fileDownload(r.data, file)

【问题讨论】:

    标签: fastapi


    【解决方案1】:

    想通了。我的 react 响应必须是 responseType: 'blob' 才能正确获取它

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 2015-12-15
      • 1970-01-01
      • 2020-10-08
      • 2020-05-04
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多