问题描述
使用asp.net Web API核心时,将图像文件作为IActionResult返回的最佳方法是什么?我尝试返回一个base64字符串,它工作正常.但不算有效.有没有一种方法可以将图像文件对象本身作为IActionResult返回.
What is the best way to return an image file as IActionResult while using asp.net web api core?I tried returning a base64 string and it works fine. But not considered as efficient.Is there a way using which we can return an image file object itself as IActionResult.
推荐答案
您可以在从Controller
或ControllerBase
继承的控制器中使用File()
函数的各种重载.
You can use the various overloads of the File()
function in controllers that inherit from Controller
or ControllerBase
.
例如,您可以执行以下操作:
For example, you can do:
return File("~/Images/photo.jpg", "image/jpeg");
这使用虚拟路径,其他选项包括为其提供字节数组或Stream
.如果需要,您还可以提供下载文件名作为第三个参数.
This uses a virtual path, other options include giving it a byte array or a Stream
. You can also give a download file name as a third argument if that is needed.
这篇关于从asp.net Web API核心返回图像作为IActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!