ZetCode

ASP.NET send image

last modified October 5, 2022

In this article we show how to send images from an ASP.NET application.

ASP.NET is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, web applications. It is developed by Microsoft.

HTTP Content-Disposition

The Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser or as an attachment, that is downloaded and saved locally.

Media type

A media type or MIME type is a specific metadata information which indicates the nature and format of a file. Servers can send various types of files including text files, HTML files, PDF, Excel or image files.

ASP.NET send image inline

In the first example, we send an image inline.

The Results.File function writes the file at the specified path to the response.

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/image", () =>
{
    var mimeType = "image/png";
    var path = Path.Combine(builder.Environment.ContentRootPath, 
        "images/sid.png");

    return Results.File(path, contentType: mimeType);
});

app.Run("http://localhost:3000");

We have one endpoint which sends an image. We do not specify the Content-Disposition; the default is inline.

var mimeType = "image/png";

We define a mime type. It is a PNG image.

var path = Path.Combine(builder.Environment.ContentRootPath, 
    "images/sid.png");

We build the path to the image.

return Results.File(path, contentType: mimeType);

We return the image to the client with Results.File.

ASP.NET send image as attachment

In the next example, we send the image as an attachment.

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/image", (HttpResponse response) =>
{
    var mimeType = "image/png";
    var path = Path.Combine(builder.Environment.ContentRootPath, 
        "images/sid.png");

    response.Headers.Add("Content-Disposition", "attachment;filename=sid.png");
    return Results.File(path, contentType: mimeType);
});

app.Run("http://localhost:3000");

We need to set the Content-Disposition to attachment.

app.MapGet("/image", (HttpResponse response) =>

To change a header, we need the HttpResponse object.

response.Headers.Add("Content-Disposition", "attachment;filename=sid.png");

We add the Content-Disposition header.

ASP.NET send image with view

The following example shows how to send an image within a static HTML file.

Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

app.UseStaticFiles();
app.UseRouting();

app.UseEndpoints(endppoints =>
{
    endppoints.MapDefaultControllerRoute();
});

app.Run("http://localhost:3000");

We have a MVC web application. We set up routing, controllers and views.

app.UseStaticFiles();

We enable static file serving. The static files are located in wwwroot directory by default.

Controllers/HomeController.cs
using Microsoft.AspNetCore.Mvc;

namespace ServeImageEx.Controllers;

public class HomeController : Controller
{
    [HttpGet("image")]
    public IActionResult Image()
    {
        return View();
    }
}

We have one action in the HomeController. It simply returns a view.

Views/Home/Image.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image</title>
</head>
<body>

    <h2>Sid the sloth</h2>

    <p>
        <img src="~/images/sid.png">
    </p>

</body>
</html>

In the view file, we have an img tag. It points to the PNG file in wwwroot/images directory.

In this article, we have served image files from an ASP.NET application.