简体   繁体   English

使用Azure Function(.NET Core)下载文件

[英]Using Azure Function (.NET Core) to Download a file

I have created and HTTP Triggered Azure Function (v2) using .NET Core with the hopes that I can execute the function while passing in some info in the request body and then have the function return/download a file in the browser. 我已经使用.NET Core创建和HTTP触发了Azure函数(v2),希望我可以在请求正文中传递一些信息的同时执行该函数,然后让该函数在浏览器中返回/下载文件。 Unfortunately I am struggling to get this working. 不幸的是,我正在努力使这个工作。

Below is a snippet of Code 以下是一段代码

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);

    req.HttpContext.Response.Headers.Add("content-disposition", "attachment;filename=Export.csv");
    req.HttpContext.Response.ContentType = "application/octet-stream";

    return (ActionResult)new OkObjectResult(filebytes);
}

When I do a post using Postman the request is accepted but the response is 406 "unacceptable" and the output in the log states 当我使用Postman进行发布时,请求被接受,但响应为406“不可接受”,并且输出为日志状态

"Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector[1] No output formatter was found for content type 'application/octet-stream' to write the response." “ Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector [1]找不到内容类型'application / octet-stream'的输出格式化程序,无法写入响应。”

I've tried multiple content types including text/plain and text/csv, all give the same response about output formatting. 我尝试了多种内容类型,包括text / plain和text / csv,它们都对输出格式给出相同的响应。

If I remove or comment out the ContentType the request processes and returns a 200 but the filebytes are returned in the response body instead of being downloaded in the browser. 如果我删除或注释掉ContentType,则请求将处理并返回200,但文件字节将在响应正文中返回,而不是在浏览器中下载。

You'll need a FileContentResult for this: 您将需要一个FileContentResult:

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"
    };
}

While the comments correctly point out that the ideal solution is to kick off processing in the HTTP function asynchronously, return a 202 Accepted response, save the result to blob storage, have the client wait for processing to complete before starting the blob download and then delete the blob once it's been downloaded, current Azure Functions pricing is only $0.000016/GB-s so you may find that to be unnecessarily complicated unless you have quite high traffic. 尽管注释正确地指出,理想的解决方案是异步启动HTTP函数中的处理,返回202 Accepted响应,将结果保存到Blob存储中,让客户端等待处理完成,然后再开始Blob下载,然后删除blob一旦下载完毕,当前的Azure Functions定价仅为$ 0.000016 / GB-s,因此除非流量非常大,否则您可能会发现不必要的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM