This entry has been published on 2018-10-23 and may be out of date.
Last Updated on 2018-10-23.
[:en]By default, returning a binary file via a Controller’s method is quite simple, like:
[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:
[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);
}
[:]