fastapi - 使用 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)
解决方案
弄清楚了。我对 react 的响应必须是 responseType: 'blob' 才能正确获取
推荐阅读
- jvm - 为 Java 编写的引导加载程序
- apache-nifi - 如何使用状态更新从 dev 到 prod 的流程
- java - Spring Boot invalidDataAccessApiUsageException - OUT/INOUT 参数不可用
- php - 我怎样才能把剩下的数据放在一些 PHP 下
- javascript - javascript计算器第一次工作,但如果我输入其他值,她只有在刷新页面后才能工作。为什么?
- python - 导入时间戳时给出错误
- powershell - 在 Powershell 启动时设置 Powershell 解析器的行为
- solr - solr - 基本身份验证
- python - Discord Python Bot 从语音频道获取成员数量
- java - Java - 列表 LocalTime 的排序列表