0

I've tried using stuff from System.Drawing but that is not available with ASP.NET. I've tried File.Copy() but that's not available either. Any help is appreciated.

this is the relevant code


var fileDetailExpanded = await _context.FileDetails.SingleOrDefaultAsync(m => m.FileDetailID == id);
if (fileDetailExpanded == null)
{
     return NotFound();
}
byte[] fileBytes = fileDetailExpanded.File;
string fileExt = fileDetailExpanded.FileType;

/* src = "~/images/displayImage.jpg */

mason
  • 31,774
  • 10
  • 77
  • 121
  • You're missing some relevant context. What is the ultimate goal here? To display the image on a webpage? Well you've got a couple options if your image is coming from the database. You could [use a data URI](https://stackoverflow.com/questions/6826390/how-to-convert-image-to-data-uri-for-html-with-c) to embed the picture info directly in the image's src attribute. Or you could save the file to a location served by your web server, then update the src to point to the location that the file is served at. – mason Apr 01 '20 at 22:49
  • Or you could change the URL to point to an endpoint (such as a generic handler) that the file is served at. That generic handler (or perhaps an MVC action method) could return a byte array that contains the file contents. The handler would know which file to return based on information in the URL. – mason Apr 01 '20 at 22:50
  • Are you using ASP.NET Web Forms? MVC 5? ASP.NET Core MVC? You should tag your question with the appropriate framework. – mason Apr 01 '20 at 22:51
  • Note that your title is probably inaccurate. You're not trying to overwrite a byte array somewhere, right? You're simply wanting to display on your webpage an image that comes from the database, right? And I'm assuming by MSSMS you mean "Microsoft SQL Server Management Studio" but that's not accurate either. We typically abbreviate that as SSMS, and SSMS is simply a client that can access and manipulate SQL Server: the image itself comes from SQL Server, not SSMS. – mason Apr 01 '20 at 22:57
  • @mason - you are correct. What I really want to do is display an image from my SQL database on my webpage. – jazerlaser Apr 01 '20 at 23:08

1 Answers1

0

So usually in ASP.NET Core MVC we would create a dedicated action method that's only job is to return the image.

public ActionResult ProductImage(int id)
{
    var fileInfo = //get this object from the database based on the id, looks like you already know how
    return File(fileInfo.TheByteArray, fileInfo.MimeType);
}

In the above code, I'm assuming you've got an object called fileInfo that contains the byte array representing the image, and the MIME type of the image (if you've only got the file extension, you could just implement a method to convert from the extension to a MIME type). You will likely have different property names, adjust your code accordingly. This action method is going to write the image directly to the response, with the MIME type passed as the content type.

By hitting this URL for image, you should be able to see the image in your browser. Then the only remaining step is to set the image element in your Razor page to point to this location. I'll assume your model has FileId property that represents the file you want the user to see.

<img src="/MyController/ProductImage/@Model.FileId" />

Note that getting an image from the server and returning it every time they hit this page might not be super efficient, so you might look into various caching options.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Thank you. I was able to get it working and through research into MIME types and – jazerlaser Apr 02 '20 at 16:53