使用.Net内核和Angular 8从数据库获取图像(二进制数据)
我将图像作为二进制数据存储在数据库中,我想返回此图像以供客户端显示,但问题是我编写的代码返回的是json数据,而不是图像,我如何返回图像作为.NET Core 3.1中使用Angular 8的客户端的图像 //注意文档是我的图像的列 这是我的密码:使用.Net内核和Angular 8从数据库获取图像(二进制数据),angular,image,aspnetboilerplate,.net-core-3.1,Angular,Image,Aspnetboilerplate,.net Core 3.1,我将图像作为二进制数据存储在数据库中,我想返回此图像以供客户端显示,但问题是我编写的代码返回的是json数据,而不是图像,我如何返回图像作为.NET Core 3.1中使用Angular 8的客户端的图像 //注意文档是我的图像的列 这是我的密码: public async Task<dynamic> GetImage(int id) { string imageBase64Data =Convert.ToBase64String(_repo
public async Task<dynamic> GetImage(int id)
{
string imageBase64Data =Convert.ToBase64String(_repository.Get(id).document);
string imageDataURL =string.Format("data:image/jpg;base64,{0}",imageBase64Data);
return imageDataURL;
}
public异步任务GetImage(int-id)
{
string imageBase64Data=Convert.ToBase64String(_repository.Get(id).document);
字符串imageDataURL=string.Format(“数据:image/jpg;base64,{0}”,imageBase64Data);
返回imageDataURL;
}
我使用的是ABP框架,我的类是ApplicationService基类,我是从服务器端(.Net Core)找到它的,我使用了FileStreamResult这将返回一个链接,使您能够加载文件,或者您可以使用FileContentResult直接返回文件
//myImage.document is a byte[]
public async Task<FileStreamResult> GetDoc(int id)
{
var myImage = await _repository.GetAsync(id);
var stream = new MemoryStream(myImage.document);
return new FileStreamResult(stream, "image/jpeg" /*chnage this based on your image type */ )
{
FileDownloadName = "test.jpeg"
};
}
//myImage.document是一个字节[]
公共异步任务GetDoc(int-id)
{
var myImage=await\u repository.GetAsync(id);
var stream=newmemoryStream(myImage.document);
返回新文件streamresult(stream,“image/jpeg”/*根据您的图像类型对其进行更改*/)
{
FileDownloadName=“test.jpeg”
};
}
到目前为止,我的印象是,为ApplicationService生成的APB动态Api控制器没有构建返回FileStreamResult-s的Api操作的约定
所以,为了从基于APB的Api下载文件,我将为此手动创建控制器,注入所需的服务,该服务将返回Stream或byte[]数组
[Route("api/[controller]/[action]")]
public class ImageController: AbpController
{
private readonly IImageStore _imageStore;
public ImageController(IImageStore imageStore)
{
_imageStore = imageStore;
}
[HttpGet]
[FileResultContentType("image/jpeg")]
public async Task<FileStreamResult> GetImageStream(string imageId)
{
Stream imageStream = await _imageStore.GetImage(imageId);
imageStream.Seek(0, SeekOrigin.Begin);
var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName($"{imageId}.jpg");
Response.Headers.Add(HeaderNames.ContentDisposition, contentDisposition.ToString());
return new FileStreamResult(imageStream, "image/jpeg");
}
}
[路由(“api/[控制器]/[操作]”)
公共类图像控制器:AbpController
{
私有只读IImageStore\u imageStore;
公共图像控制器(IImageStore图像存储)
{
_imageStore=imageStore;
}
[HttpGet]
[FileResultContentType(“图像/jpeg”)]
公共异步任务GetImageStream(字符串imageId)
{
Stream imageStream=wait_imageStore.GetImage(imageId);
Seek(0,SeekOrigin.Begin);
var contentDisposition=新ContentDispositionHeaderValue(“附件”);
SetHttpFileName($“{imageId}.jpg”);
Add(HeaderNames.ContentDisposition,ContentDisposition.ToString());
返回新的FileStreamResult(imageStream,“image/jpeg”);
}
}
请注意一个重要属性,使其能够与nswagFileResultContentType
配合使用。有关更多信息,请参阅本帖: