By default, returning a binary file via a Controller’s method is quite simple, like:
1 2 3 4 5 6 7 8 9 |
[Route("Dynamic/{token}")] public IActionResult Dynamic(String token) { var item = cache.Where(o => o.Token == token).FirstOrDefault(); //jpg, png if (item.ImageBytes != null) return File(item.ImageBytes, "image/" + item.ImageType); } |
However, this does NOT work if you try to return a text file like XML or an SVG image. It will end up in a Controller error.
Solution
Convert the text to binary:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Route("Dynamic/{token}")] public IActionResult Dynamic(String token) { var item = cache.Where(o => o.Token == token).FirstOrDefault(); //jpg, png if (item.ImageBytes != null) return File(item.ImageBytes, "image/" + item.ImageType); //svg return File(Encoding.UTF8.GetBytes(item.ImageXML), "image/"+item.ImageType); } |
Comments