首页 > 解决方案 > 使用 fastAPI 提供文件的问题

问题描述

我正在尝试从 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

解决方案


弄清楚了。我对 react 的响应必须是 responseType: 'blob' 才能正确获取


推荐阅读