Sat*_*jit 6 asp.net-core asp.net-core-webapi
使用asp.net web api核心时,将图像文件作为IActionResult返回的最佳方法是什么?我尝试返回一个base64字符串,它工作正常.但不认为有效.是否有一种方法可以将图像文件对象本身作为IActionResult返回.
juu*_*nas 10
您可以File()在继承自Controller或的控制器中使用该函数的各种重载ControllerBase.
例如,你可以这样做:
return File("~/Images/photo.jpg", "image/jpeg");
Run Code Online (Sandbox Code Playgroud)
这使用虚拟路径,其他选项包括给它一个字节数组或一个Stream.如果需要,您还可以将下载文件名作为第三个参数.
小智 6
[Route("getProductImage/v1")]
[HttpGet]
public async Task<IActionResult> getProductImage(GetProductImageQueryParam parammodel)
{
using (HttpClient client = new HttpClient())
{
MNimg_URL = MNimg_URL + parammodel.modelname;
HttpResponseMessage response = await client.GetAsync(MNimg_URL);
byte[] content = await response.Content.ReadAsByteArrayAsync();
//return "data:image/png;base64," + Convert.ToBase64String(content);
return File(content, "image/png", parammodel.modelname);
}
}
Run Code Online (Sandbox Code Playgroud)
在.net core web api中你可以使用上面的代码
这里 GetProductImageQueryParam 是一个带有输入参数的类
| 归档时间: |
|
| 查看次数: |
9248 次 |
| 最近记录: |