gpt4 book ai didi

c# - 如何在C#中读取图像和文本值?

转载 作者:行者123 更新时间:2023-12-03 01:45:31 26 4
gpt4 key购买 nike

enter image description here

如您所见,我正在尝试通过 POST 命令将图像和名称发送到本地函数。

如何在 C# 中读取这两个参数?

这是我尝试过的,但它只能读取文件图像。

   [FunctionName("Test")]
public static async Task<HttpResponseMessage>
Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route =
null)]HttpRequestMessage req, TraceWriter log)
{
//Check if the request contains multipart/form-data.
if (!req.Content.IsMimeMultipartContent())
{
return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}


foreach (var stream in contents.Contents)
{


try
{
var fileBytes = await stream.ReadAsByteArrayAsync();
var fileinfo = new FileInfo(stream.Headers.ContentDisposition.FileName.Trim('"'));



//Can Read File image like this.


}
catch(Exception e)
{
return req.CreateErrorResponse(HttpStatusCode.Conflict, e);
}

最佳答案

Is there a workaround to do this like using memory stream?

根据您的要求,我假设您可以使用HttpContentMultipartExtensions.ReadAsMultipartAsync您将获得 MultipartMemoryStreamProvider,然后您可以利用以下代码来读取上传的文件:

var multipartMemoryStreamProvider= await req.Content.ReadAsMultipartAsync();
foreach (HttpContent content in multipartMemoryStreamProvider.Contents)
{
// for reading the uploaded file
var filename= content.Headers.ContentDisposition.FileName.Trim('"');
var stream=await content.ReadAsStreamAsync();

//for formdata, you could check whether `content.Headers.ContentDisposition.FileName` is empty
log.Info($"name={content.Headers.ContentDisposition.Name},value={await content.ReadAsStringAsync()}");
}

此外,您可以关注此issue关于基于 MultipartMemoryStreamProvider 创建自定义 MultipartFormDataMemoryStreamProvider。还有这个issue关于基于 MultipartStreamProvider 构建自定义 InMemoryMultipartFormDataStreamProvider。

关于c# - 如何在C#中读取图像和文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com