【发布时间】:2019-06-19 20:48:31
【问题描述】:
我正在从 WebApi 中的文件夹中获取图像并尝试在我的 Angular 应用程序中呈现它。这是从其文件夹中获取图像的网络服务代码:
[HttpGet]
[Route("GetProfileImage")]
[Authorize]
public IActionResult GetImage()
{
try
{
var userId = User.Claims.First(c => c.Type == "UserID").Value;
var photo = _applicationContext.files.FirstOrDefault(x => x.UserId == userId);
var fileName = photo.FileName;
var uploadFilesPath = Path.Combine(_iHostingEnvironment.ContentRootPath, "uploads");
var filePath = Path.Combine(uploadFilesPath, fileName);
return Ok(PhysicalFile(filePath, "image/jpeg"));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
这是后端调用 webapi 的代码:
component.ts
getProfileImage() {
this.userService.getProfileImage()
.subscribe((data: any) => {
this.imageUrl = data.fileName;
console.log(data.fileName);
});
}
service.ts
public getProfileImage = () => {
return this.http.get(this.baseUrl + '/api/ApplicationUser/GetProfileImage');
}
component.html
<div class="card-avatar" *ngIf="imageUrl">
<a href="javascript:void(0)" >
<img class="img" src="imageUrl" />
</a>
</div>
当我 console.log 回复时,我会得到文件本身的物理位置,如下所示:
D:\backend\uploads\ca897a19-d8f6-4f77-8445-927c29f9f86d.jpg
但它没有呈现在我的 .html 文件中的 src 中。
你能告诉我如何正确地做吗?谢谢。
【问题讨论】:
-
您也可以只让 src 指向动作,即
this.baseUrl + '/api/ApplicationUser/GetProfileImage',并让它返回原始数据以供图像标签呈现