10

I'm working on minimal api, what I'm trying is when the user visits /download it immediately downloads my picture named add.png.

But no matter what I try it doesn't work because I either get an empty page with only {}

Is this possible? if so how

This is my code that I've tried so far. (I got access denied with all permissions on the location!)

app.MapGet("/download", async () =>
  {
      var path = "add.png";
      using (var stream = new FileStream(path, FileMode.Open))
      {
          stream.CopyToAsync(stream);
      }
      var ext = Path.GetExtension(path).ToLowerInvariant();
      var result = (ext, Path.GetFileName(path));
      return result;
  });

How do I do this for when the user does /download within my api that he is going to download a file?

Thanks in advance

Teun
  • 161
  • 1
  • 1
  • 8

3 Answers3

20

You can use Results.File to return file to download from your Minimal APIs handler:

app.MapGet("/download", () =>
{
    var mimeType = "image/png";
    var path = @"path_to_png.png";
    return Results.File(path, contentType: mimeType);
});
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • This is not working for me... Not sure why. var mimeType = "application/zip"; var path = @$"C:\logs\{fileName}.zip"; return Results.File(path, contentType: mimeType); – Benjamin McGill Jun 21 '22 at 18:57
  • @BenjaminMcGill "not working" is not very informative. – Guru Stron Jun 21 '22 at 19:59
  • I followed it up with an answer below. But you are right, "not working" is not very descriptive. In my case, the download would not start. I could see the call being made to the api, the api responded with the debug messages, but the download would not start. – Benjamin McGill Jul 07 '22 at 15:06
3

For some reason the accepted answer did not work for me. Maybe because I am downloading a zip file... Here's what ended up working for me...

        app.MapGet("download/{fileName}", async (string fileName) => 
        {
            var mimeType = "application/zip";
            var path = @$"{fileName}.zip";
            
            var bytes = await File.ReadAllBytesAsync(path);

            return Results.File(bytes, mimeType, $"{fileName}.zip");

        })
            .Produces(StatusCodes.Status200OK)
            .Produces(StatusCodes.Status404NotFound)
            .WithName($"GET {EndpointCategory} by Name")
            .WithTags(Tag);
Benjamin McGill
  • 359
  • 3
  • 5
0

I tried in 3 different ways to download a file using asp.net core minimal api . One thing I was not able to figure out is that while (1) and (2) was downloading the file from the root level , (3) was downloading from wwwroot folder. If anyone can clear this it would be helpful.

please see the project folder structure image - project folder structure

app.MapGet("/showxwingdetails", () =>
    {
        // ( 1 )
        //byte[] barr =  File.ReadAllBytes("xwing.jpg");
        //return Results.File(barr, "image/jpg", "xwing.jpg");
    
        // ( 2 )
        //FileStream fs = File.OpenRead("xwing.jpg");   
        //return Results.File(fs, "image/jpg", "xwing.jpg");
    
        // ( 3 ) 
        // return Results.File("xwing.jpg", "image/jpg", "xwing.jpg");
    
    });
bucket
  • 26
  • 1
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/33828758) – Moritz Ringler Feb 17 '23 at 13:09