从asp.net Web API核心返回图像作为IActionResult

编程入门 行业动态 更新时间:2024-10-25 12:18:30
本文介绍了从asp Web API核心返回图像作为IActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用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

本文发布于:2023-10-29 07:48:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   核心   net   asp   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!