post - 是否可以在使用 Actix-Multipart 完全传输大型 POST 请求之前发送响应?
问题描述
我正在查看actix-multipart 示例,并且有很多阻塞文件 IO。是否可以在下载文件之前发送响应?多部分不是Send
。我想做这样的事情:
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
async_std::task::spawn(async {
// iterate over multipart stream
while let Some(item) = payload.next().await {
let mut field = item?;
let content_type = field.content_disposition().unwrap();
let filename = content_type.get_filename().unwrap();
let filepath = format!("./tmp/{}", filename);
// File::create is blocking operation, use threadpool
let mut f = web::block(|| std::fs::File::create(filepath))
.await
.unwrap();
// Field in turn is stream of *Bytes* object
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
// filesystem operations are blocking, we have to use threadpool
f = web::block(move || f.write_all(&data).map(|_| f)).await?;
}
}
});
Ok(HttpResponse::Ok().into())
}
解决方案
推荐阅读
- azure - Azure 上的 IdentityServer 4 我们能否使用 Azure API 网关保护其端点
- python - 我想在 json body 元素中发布一个对象列表
- c# - 在 Xamarin.Form 页面之间传递 Int C#
- c - 使用 STRCMP 函数在结构中搜索日期
- validation - Vue 中使用 vue-signature-pad 的验证问题
- java - 流以填充双精度的二维数组
- c# - 无法保存使用 DI API 的销售员工的某些字段,例如“电话”、“电子邮件”、“税”、..
- javascript - 在 Google 跟踪代码管理器的 1*1 像素广告中添加的 HTML 代码未显示在页面上
- python - 使用 Telethon 从电报组下载特定数量的消息
- magento2 - 从 CMS 块调用 .phtml 文件 - Magento 2