Home > Software design >  How to display both svg and png images from database?
How to display both svg and png images from database?

Time:09-12

So I have some images stored in the database. They consist of both png and svg images. But I cannot display the svg images but the png images are displayed correctly.

public ActionResult GetImage() {
    // some code here
    return new FileContentResult(image, "image/png");
}

How can I display both svg and png?

CodePudding user response:

The svg images was not displayed because of the wrong content type in FileContentResult.

So the solution is to find the content type:

public ActionResult GetImage() {
    // some code here
    var contentType = GetContentType(image);
    return new FileContentResult(image, contentType);
}

private string GetContentType(byte[] image)
{
    var returnType = "image/png";
    var fileheader = Encoding.UTF8.GetString(image, 0, 25);
    if (fileheader.StartsWith("<?xml ") || fileheader.StartsWith("<svg ") || fileheader.StartsWith("\r\n<svg "))
        returnType = "image/svg xml";
    return returnType;
}
  • Related