Dex*_*ity 2 c# azure azure-functions
我正在调整最近在 .NET Standard 中启动的项目以使用 Azure Functions。我有一个 HTTP 触发器,我直接从表单发布到该触发器。只有两个字段:数字输入和文件上传。
当使用该函数而不引用任何其他库时,我无法在 HttpRequestMessage 上使用 ReadAsFormDataAsync。我收到:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is
available to read an object of type 'FormDataCollection' from content with
media type 'multipart/form-data'.
Run Code Online (Sandbox Code Playgroud)
我可以使用 ReadAsMultipartAsync 并设法获取发布值。
但是,当我引用 .NET 标准库时,我什至无法输入该函数,因为它被完全拒绝:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is
available to read an object of type 'HttpRequestMessage' from content with
media type 'multipart/form-data'
Run Code Online (Sandbox Code Playgroud)
我尝试创建一个全新的框架 .NET 标准库并引用它和同样的东西。
作为附加参考,我找到了这篇文章,但我似乎没有遇到同样的问题。
本来打算提交一个问题,但决定先在这里尝试一下。有任何想法吗?
编辑:当 enctype 为 application/x-www-form-urlencoded 时,也会发生这种情况。
据我所知,“ReadAsFormDataAsync”方法仅接受“application/x-www-form-urlencoded”类型的内容。它不支持获取“multipart/form-data”类型的内容。
因此,如果您想发送比赛的多个部分,则需要使用“ReadAsMultipartAsync”方法。
有关如何在azure函数中使用“ReadAsMultipartAsync”方法的更多详细信息,您可以参考以下代码:
using System.Net;
using System.Net.Http;
using System.IO;
using System.Collections.Specialized;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string result = "- -";
if (req.Content.IsMimeMultipartContent())
{
var provider = new MultipartMemoryStreamProvider();
req.Content.ReadAsMultipartAsync(provider).Wait();
foreach (HttpContent ctnt in provider.Contents)
{
//now read individual part into STREAM
var stream = ctnt.ReadAsStreamAsync();
return req.CreateResponse(HttpStatusCode.OK, "Stream Length " + stream.Result.Length);
using (var ms = new MemoryStream())
{
//do something with the stream
}
}
}
if (req.Content.IsFormData())
{
NameValueCollection col = req.Content.ReadAsFormDataAsync().Result;
return req.CreateResponse(HttpStatusCode.OK, $" {col[0]}");
}
// dynamic data = await req.Content.ReadAsFormDataAsync();
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "Doesn't get anything " + result);
}
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
9345 次 |
| 最近记录: |