使用asp Web API核心时,将图像文件作为IActionResult返回的最佳方法是什么? 我尝试返回一个base64字符串,它工作正常.但不算有效. 有没有一种方法可以将图像文件对象本身作为IActionResult返回.
What is the best way to return an image file as IActionResult while using asp 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
发布评论