Is it possible to download files to a user's local machine from a function app?

Matt Schwartz 20 Reputation points
2024-09-23T20:13:35.73+00:00

I would like to give users the ability to download a file to their local machine from our web app.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,978 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,316 Reputation points
    2024-09-23T20:26:24.4733333+00:00

    That depends on a lot of factors. In general function apps should be used to replace things that you would normally have implemented as a service or as a scheduled job. Hence they tend to be time-trigger based. In this case there is no user so nothing to download to.

    If you are implementing an HTTP-trigger function then you're building a mini-API service. In this case you could return a file as it is a normal HTTP response as discussed here. However that may not scale well depending on the size of the file and what it takes to generate it. Unless you have a single endpoint and just need to generate a single file then an Azure Function may not be the best way to go here. You would probably do better to build an actual API application.

    An alternative approach to returning the file is to use the Azure Storage binding to store the file in Storage. Then generate a link to the generated file, or to something that can get it from storage if the storage is accessible to your clients. You can then optimize multiple calls to get the same file. It also eliminates the problems you'll run into if you try to download large files and/or need to support range-based downloads.


1 additional answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,556 Reputation points MVP
    2024-09-23T22:05:41.97+00:00

    Hello @Matt Schwartz,welcome to this moderated Azure community forum.

    I agree with the comment @Michael Taylor made, there are better solutions than using a function app (better scalable, better security, etc.).

    Still, it is possible using the 'FileContentResult' as seen here:

    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, ILogger log)
    {
        string csv;
    
        //Do some stuff to create a csv
    
        byte[] filebytes = Encoding.UTF8.GetBytes(csv);
    
        return new FileContentResult(filebytes, "application/octet-stream") {
            FileDownloadName = "Export.csv"
        };
    }
    

    I personally would go for the storage account solution where a temporarily SAS token is generated with access right for a a download.

    Check out this blog post with details on how to do this.

    This is much more scalable and traceability could be added. The only challenge is that custom code must be run locally to do the actual download.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.