如何在 azure 函数中读取文件(在发布请求中)?
Posted
技术标签:
【中文标题】如何在 azure 函数中读取文件(在发布请求中)?【英文标题】:How to read a file (in a post request) in azure functions? 【发布时间】:2021-11-16 01:33:09 【问题描述】:我通过 Post 请求发布了一个 txt 文件,我想读取它并将其存储在一个字符串中,以便我可以在该字符串上使用认知服务。我尝试将动态数据变量转换为字符串,但是当我记录它时,它是空白的,streamreader 也是如此。流式阅读器不应该有一些文字吗?
下面是我的代码。
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;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using Microsoft.Azure.ServiceBus;
using System.Text;
namespace Demo1
public static class Function1
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
log.LogInformation("C# HTTP trigger function processed a request.");
Person p = new Person();
string fname = req.Query["fname"];
string lname = req.Query["lname"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
log.LogInformation("data to store is" + requestBody);
dynamic data = JsonConvert.DeserializeObject(requestBody);
//fname = fname ?? data?.fname;
//lname = lname ?? data?.lname;
string text = Convert.ToString(data);
log.LogInformation("data to store is"+text);
if (!string.IsNullOrEmpty(fname))
p.firstName = fname;
p.lasttName = lname;
p.text = text;
//string filename = fname+".txt";
Guid g = Guid.NewGuid();
string filename = g + ".txt";
await CreateBlob(filename, p, log, filename);
string responseMessage = string.IsNullOrEmpty(fname)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, fname lname.";
return new OkObjectResult(responseMessage);
【问题讨论】:
查看您的代码,您的函数似乎需要一个 JSON 主体。那是您在 POST API 调用中传递的内容吗?您能否使用 POST API 调用的截图来编辑问题(可能通过邮递员)。还有 CreateBlob 函数在哪里? 【参考方案1】:要在发布请求中读取文件,您可以使用Http Request Form
获取文件详细信息。
使用InputStream
读取文件。检查以下代码以供参考。
using System;
using System.IO;
using System.Web;
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 functionreadfile
public static class Function1
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
log.LogInformation("C# HTTP trigger function processed a request.");
var formdata = await req.ReadFormAsync();
var filePath = "D:\\";
string name = formdata["name"];
var textfile = req.Form.Files["file"];
if (textfile.Length > 0)
using (var inputStream = new FileStream(filePath + textfile.FileName, FileMode.Create))
// read file to stream
await textfile.CopyToAsync(inputStream);
// stream to byte array
byte[] array = new byte[inputStream.Length];
inputStream.Seek(0, SeekOrigin.Begin);
inputStream.Read(array, 0, array.Length);
// get file name
string fName = textfile.FileName;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, name. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
您可以在代码中使用它来读取文件。
【讨论】:
以上是关于如何在 azure 函数中读取文件(在发布请求中)?的主要内容,如果未能解决你的问题,请参考以下文章
Azure:无法从 Azure 函数 HttpTrigger 读取 Blob 文本(400 - 错误请求错误)
使用 azure 函数从表单数据请求中详细说明并存储在 azure blob 中的多个文件