Mattman85208
Mattman85208

Reputation: 2100

ASP.NET Core Convert byte array to svg image in a View

Using ASP.Net Core netcore 3.1 I have an SQL Server Table with a column of varbinary(MAX) with files uploaded into it. (Was done in ColdFusion) Now with the .Net core I want to display the svg images in the browser. Here is some more info:

In the Model class:

public byte[] FileContainer { get; set; }  <-- this is the column with the file data

In the Controller it's just a simple:

List<TableName> userFiles = await _conn.TableName.Where(u => u.OwnerID == ID).ToListAsync();
 return View(userFiles);

In the view:

@model IEnumerable<Projectname.Models.TableName>
…
@foreach (var item in Model){
<div style='width:12px;'>
    @Html.Raw(item.FileContainer)
</div>

This just results in "System.Byte[]" being displayed in the browser. How do I make it display the svg file?

Upvotes: 0

Views: 1108

Answers (1)

Mattman85208
Mattman85208

Reputation: 2100

Add at the top of the View:

@using System.Text

Then do this:

@Html.Raw(Encoding.UTF8.GetString(item.FileContainer))

Works like a champ (tested in FireFox)

Upvotes: 1

Related Questions