使用 Azure Function (.NET Core) 下载文件 [英] Using Azure Function (.NET Core) to Download a file

查看:36
本文介绍了使用 Azure Function (.NET Core) 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 .NET Core 创建和 HTTP 触发的 Azure 函数 (v2),希望我可以在传递请求正文中的一些信息的同时执行该函数,然后让该函数在浏览器中返回/下载文件.不幸的是,我正在努力让它发挥作用.

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. Unfortunately I am struggling to get this working.

下面是代码片段

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

当我使用 Postman 发帖时,请求被接受,但响应为 406不可接受"并且日志中的输出状态

When I do a post using Postman the request is accepted but the response is 406 "unacceptable" and the output in the log states

"Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector[1]找不到用于写入响应的内容类型应用程序/八位字节流"的输出格式化程序."

"Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector[1] No output formatter was found for content type 'application/octet-stream' to write the response."

我尝试了多种内容类型,包括 text/plain 和 text/csv,都对输出格式给出了相同的响应.

I've tried multiple content types including text/plain and text/csv, all give the same response about output formatting.

如果我删除或注释掉 ContentType,请求会处理并返回 200,但文件字节将在响应正文中返回,而不是在浏览器中下载.

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.

推荐答案

为此你需要一个 FileContentResult:

You'll need a FileContentResult for this:

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

虽然评论正确指出理想的解决方案是异步启动 HTTP 函数中的处理,返回 202 Accepted 响应,将结果保存到 blob 存储,让客户端等待处理完成,然后再开始 blob 下载然后在下载后删除 blob,当前 Azure Functions 定价仅为 0.000016 美元/GB-s,因此您可能会发现这会变得不必要地复杂,除非您的流量很高.

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.

这篇关于使用 Azure Function (.NET Core) 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆