4

I would like to perform a file download and trigger a browser's file save dialog box from the Controller level, if possible. (to hide a file's location)

I was basing on this article in my solution.

https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core

What I get at the end is a file type(file path is correct and file type as well), but the browser is not triggered to open the dialog box to save this file. (what happens in case of the href link)

How to achieve this ?

Carl S.
  • 69
  • 1
  • 1
  • 8
  • For those struggling with the fact that the link above only shows an action method for the download, no markup or script, note that there's a link at the top of the tutorial for downloading the project at github. It's just a simple anchor that adds the filename to the parameter list and the rest is magic. – IdahoB Jul 26 '19 at 21:04

3 Answers3

5

You can do something like this.

public IActionResult Download(string filename)
{
    var file = Path.Combine(_hostingEnvironment.WebRootPath, "files", filename);
    return File(System.IO.File.ReadAllBytes(file), "application/octet-stream", filename);
}

It is reading all bytes from file and returning with application/octet-stream, which shows the download dialog box.

Other ways mentioned here

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • `return File(System.IO.File.ReadAllBytes("C:/Temp/Generic.csv"), "application/octet-stream", "Generic.csv");` Is what I'm returning and the file does exist but I'm getting zero download dialog. – Daniel Jackson Jul 13 '18 at 21:55
  • unable to found **File()** in .net core 2.0. may i know the namespace or nuget package needs to add? – Hardik Leuwa Aug 13 '18 at 13:15
  • You don't need to add any package or namespace if I am not wrong. Hope you're inheriting from controller class. – Anuraj Aug 13 '18 at 13:18
  • A little out of date ... but the best answer is "this is a browser setting". If turned off, the files are downloaded to the default location specified in the browser settings. If turned on, then the user is asked where to save the file, e.g. the ''save as dialog". Most techniques that I've seen that attempt to override what the browser is trying to do don't work or don't work consistently across different browsers. Just my 2-bits.... – IdahoB Jul 26 '19 at 21:02
  • Hardik -> "File" is in the namespace called "System.IO"; – Carl S. Aug 13 '19 at 13:46
0

This is a full solution for the "alert.mp3" file download from wwwroot/files directory from User Controller.

View:

            <form class="form-inline" method="post" enctype="multipart/form-data" asp-controller="User" asp-action="DownloadFile">

                <input id="@item.ID" name="fileName" type="hidden" class="form-control" placeholder="@item.ID" value="alert.mp3">
                <button type="submit" style="border:hidden;background-color:transparent" data-toggle="tooltip" title="Download!">
                    <span type="submit" id="@item.ID;@item.AuthorID;Download" class="featurette-image img-fluid mx-auto glyphicon glyphicon-cloud-download" style="cursor:pointer;color:cornflowerblue;font-size: 2em;" data-toggle="tooltip" title="Download">
                    </span>
                </button>
            </form>

Controller:

    public async Task<FileResult> DownloadFile(string fileName)
    {
        var path = Path.Combine(
           Directory.GetCurrentDirectory(),
           "wwwroot\\files\\", fileName);

        var memory = new MemoryStream();
        using (var stream = new FileStream(path, FileMode.Open))
        {
            await stream.CopyToAsync(memory);
        }
        memory.Position = 0;
        return File(memory, MediaTypeNames.Application.Octet, Path.GetFileName(path));
    }

Cheers!

Carl S.
  • 69
  • 1
  • 1
  • 8
-1

Checked that this solution works (including Your answer Anuraj):

https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core

(not necessarly when I call this post request from javascript)

But when it's called directly by invoking Controller's Action from HTTP level or when it's called as Controller's Index method. Issue solved.

Carl S.
  • 69
  • 1
  • 1
  • 8
  • 1
    I don't get it. I'm returning the file but what's supposed to happen after that? The link you shared I've found already and just says Uploading and Downloading but I see no downloading happening. – Daniel Jackson Jul 13 '18 at 21:52
  • Please check new answer with complete solution. – Carl S. Jul 15 '18 at 05:47