ASP .Net Web API downloading images as binary

后端 未结 4 878
心在旅途
心在旅途 2020-12-02 19:00

I want to try to use Web API make a rest call but I want the response to be the actual binary image stored in a database, not a JSON base64 encoded string. Anyone got some p

4条回答
  •  攒了一身酷
    2020-12-02 19:31

    This task is much easily achieved without using WebAPI. I would implement a custom HTTP handler for a unique extension, and return the binary response there. The plus is that you can also modify the HTTP Response headers and content type, so you have absolute control over what is returned.

    You can devise a URL pattern (defining how you know what image to return based on its URL), and keep those URLs in your API resources. Once the URL is returned in the API response, it can be directly requested by the browser, and will reach your HTTP handler, returning the correct image.

    Images are static content and have their own role in HTTP and HTML - no need to mix them with the JSON that is used when working with an API.

提交回复
热议问题