How to to return an image with Web API Get method

前端 未结 4 1821
自闭症患者
自闭症患者 2020-12-01 08:57

I need to return an image with a Web API Get method. The code below seems to work fine except that I get this message in the Fiddler\'s ImageView window, \"This response is

4条回答
  •  半阙折子戏
    2020-12-01 09:41

    In addition to previous answers, use this to get image based on hosting environment within System.Web.Hosting namespace.

    var imageUrl = HostingEnvironment.MapPath("~/images/photo.png");
    

    and use imageUrl it to find byte[] of the image, and process with proper content-type like image/jpeg

    byte[] binaryImage = File.ReadAllBytes(imageUrl);   
    return File(binaryImage , "image/jpeg");
    

    Make sure you have referenced System.IO namespace for File static method.

提交回复
热议问题