Azure 函数文件上传和读取内容

Azure Function File Upload and Read Content

您好,我正在尝试使用 Azure Functions 的 Http 触发器来接收上传的文件并读取所述文件。

IE.

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] 
    HttpRequest req, ILogger log)

'req' 将是一个文件。

我如何read/parse请求? 该文件将是 text/word 文档或 excel(猜测必须有两个单独的代码块来处理不同的媒体类型)。

编辑:文件上传后,我想阅读文件的内容:如果文件显示:“Hello World”,我该如何阅读?

更新:

例如,如果我有一个 txt 文件:

我把它放在请求的正文中:

我将使用它来获取数据:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp25
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string a = req.ContentType;
            string content = null;
            if (req.ContentType == "text/plain")
            {
                content = await new StreamReader(req.Body).ReadToEndAsync();
            }
            return new OkObjectResult(content);
        }
    }
}

然后我会得到数据:

原答案:

可以,但是我们必须先知道输入的媒体类型。

直接获取内容类型即可。

这是常见文件格式的内容类型:

.doc      application/msword
.dot      application/msword

.docx     application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx     application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm     application/vnd.ms-word.document.macroEnabled.12
.dotm     application/vnd.ms-word.template.macroEnabled.12

.xls      application/vnd.ms-excel
.xlt      application/vnd.ms-excel
.xla      application/vnd.ms-excel

.xlsx     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx     application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm     application/vnd.ms-excel.sheet.macroEnabled.12
.xltm     application/vnd.ms-excel.template.macroEnabled.12
.xlam     application/vnd.ms-excel.addin.macroEnabled.12
.xlsb     application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt      application/vnd.ms-powerpoint
.pot      application/vnd.ms-powerpoint
.pps      application/vnd.ms-powerpoint
.ppa      application/vnd.ms-powerpoint

.pptx     application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx     application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx     application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam     application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm     application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm     application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm     application/vnd.ms-powerpoint.slideshow.macroEnabled.12

.mdb      application/vnd.ms-access

更多信息,只需搜索 MIME 类型即可。确定文件类型后,就用一些包来处理数据。

比如我在req body中放了一个docx文件

我可以这样做来获取 C# 中的内容类型:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp24
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string a = req.ContentType;

            return new OkObjectResult(a);
        }
    }
}

如果需要一个函数处理不同类型的文件,只需要将获取内容类型的逻辑放在函数体中,让不同的类型去不同的逻辑即可。