【发布时间】:2021-11-23 09:54:11
【问题描述】:
我想使用路径 /images/{pic_name} 将图像发送到前端。 这是actix web方式
.service(web::resource("/images/{pic_name}").route(web::get().to(images)))
这里是功能图片:
async fn images(req: HttpRequest, info: web::Path<Info>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/jpeg")
.body("images/".to_string() + &info.pic_name + &".jpeg"))
}
文件夹结构是:
images -| -> inside jpeg and webp
src -| -> inside the source files rs
Cargo.toml -|
我的货物 toml 是
[dependencies]
actix-web = "4.0.0-beta.5"
actix-service = "2.0.0-beta.5"
actix-files = "0.6.0-beta.8"
actix-rt = "2.4.0"
问题:在express sendFile方式上发送像nodejs这样的图像的方式是什么,
res.sendFile(path.join(__dirname, `/assets/images/${req.params.picName}.${acceptedHeader}`));
编辑:可能路径错误或其他原因,因为我得到 200 并且图像总是损坏。
【问题讨论】:
-
“我得到一个 200 并且图像总是损坏”这是因为您将文件路径作为实际响应发送,而不是该文件的内容。跨度>
-
这里是 Rust 的新手,我如何发送内容?没有找到关于此的基本示例。