asp.net core 3.1 webapi FileContentResult

今天有个场景需要webapi返回给客户端回应一个图片,以前的老套路不能用了。刚开始以为是需要使用“HttpResponseMessage“,直接在Response里输出文件流。

 

 这是错误的方向。正确的方式是使用FileContentResult,如下:

 

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("demo")]
        public FileContentResult demo()
        {
            var data = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAtHSURBVHja1FptbFPVG7/39rb29mUtfVk7y1imjFnnkAU3tw4ySUDJsjjQQXQsvuCysE0JYowBghD3gRgSFIXIGJkLSIh+2VgYL4qiDlFJQwQJTFPpZAKjW1f7cvtyb3sfP5x613/Zbl+uDP7Ph6bteXt+z3POc875PQcHAOz/WUgMw8LhcNbtAYAgCKlUShAEhmE+n29sbMztdo+NjQUCAZZlcRyXy+VardZgMBgMBr1eL5fLMQyLxWLRaJTjOBzHsx5dLpeTSIns2stkMolE4vV6z58/f+7cOYfDMTIyQhCETCYjSRIAotFoKBTy+/0ej4emaY7j9Hr9ggULqqurbTZbcXExRVHRaJRlWQDIDgkOAOFwOH0MaCSKojiOs9vtx44d+/3339VqdVlZWWlpqdlsxnE8EAh4PB6PxxMKhZByBEEwDON2ux0Ox08//eRwOEiSLCsrq6+vr6+vLyoqAoBQKBTXKT0kyLcZAEB1KIqKxWInT57s7e2VSCTLly+32WzRaNRut3/33Xd2u314eNjtdvPaJLpLr9frdLpZs2aFQqGbN2/eunULwzCtVvv888+3tLRUVFRwHBcOh+8KAAAgSVImkw0ODnZ3d+t0urVr1+bl5X311VefffbZ4OCg1+vNyPUSiQStBPSToqjGxsa33nrLarVGIhGO4/4bAGjCAIBCofB4PO+///7o6Oibb76Zn5/f1dXV2dnpdDr/w5Ci1+s3b97c3t4uk8mQDwW8gQBgaPIFpx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CQIIj/yRMjJ2R9Op8ZWbRoUSAQ4On05AQHuuBnd7qemWTZqVOnEDUtlKERyc/dPWltbUVpsmkB0DTNMMzo6GhxcfH9pn1VVdXNmzcZhuHzfCmylBRF3SeqEwRhtVrR2TMx05oiT7xv3777QXuJRPLII4/09PTwUz+DTP327dvvrfYkSc6fP//DDz9kWfZOJYUA8BkoMbyNSJHJZBUVFbt27ULpr4zfStA0jejf9957b+a1z8nJeeqpp/bu3RsKhSKRiMBrFaEXW+ixnlwu//TTTzds2ODz+WZGe4vFUl5e/sILLzz33HMcxyHa6k7mJoMnZxRF/fzzz21tbRcuXLjb08ZqtVZWVjY1NVVXVyPFpqPNM3j0h97MTUxM7NixA7n1bmg/Z86cxx57bNmyZQ0NDRaLRVh7LItXi1KpVCqVnjt3rqOjI1MiQzjM5+XlPfTQQ+Xl5fX19VVVVTiOMwyDaC+xAND/PKcQjUZJkvT5fL29vT09PRcvXkznfaQAN2wymcxmc2FhYU1NzdKlS41GI+qQZ0sRgClhpAbAax+LxViWDYfD4XDY5/Oh7fDPP//84Ycffvzxx+vXr0cikTRPy1KpFCWP1Wq1SqVSq9XI9g8//LBKpVIqlQqF4oEHHkCPlyUSCf6vTAeATHNgHMclEglBEKjTSCQilUpLS0t1Ot21a9eGh4dv377t8/mSbIHeUUskEqlUKpfLpf8KSZJKpdJoNBYUFFgsFo7jJiYmUH2SJEmSRA5PZK2nXcqJj5ancwKGYfwUYhgGBeZAIPD333+73e6JiYmxsbGxsTGXyzU+Pu7xeAKBQDAYRPOYIAikFrK9Wq3W6XR5eXlms9lgMOh0Oq1Wq9Vqc3Jy1Gq1XC6XyWQIM2orDICiqH8GAOMC1t/n4mINAAAAAElFTkSuQmCC";
            var buffer = Convert.FromBase64String(data);
            return new FileContentResult(buffer, "image/png");
        }

调用

 

posted @ 2020-02-05 22:40  数据酷软件  阅读(815)  评论(0编辑  收藏