C# 将文件添加到Flurl多部分POST请求时,IFormFileCollection为null

C# 将文件添加到Flurl多部分POST请求时,IFormFileCollection为null,c#,asp.net-core,multipartform-data,flurl,iformfile,C#,Asp.net Core,Multipartform Data,Flurl,Iformfile,我正在尝试使用Flurl通过AddFile上传文件 生成的IFormFileCollection为空,尽管我在查看内容长度正确的Request.Form.Files[0]时可以看到该项 创建请求: public Task<HttpResponseMessage> UploadImage(string fileName, MemoryStream stream) { stream.Position = 0; _baseUrl .AppendPathSe

我正在尝试使用Flurl通过
AddFile
上传文件

生成的
IFormFileCollection
为空,尽管我在查看内容长度正确的
Request.Form.Files[0]
时可以看到该项

创建请求:

public Task<HttpResponseMessage> UploadImage(string fileName, MemoryStream stream)
{
    stream.Position = 0;

    _baseUrl
        .AppendPathSegments("uploadImage")
        .PostMultipartAsync(mp => mp
            .AddFile("files", stream, fileName))
}
[HttpPost]
[Route("uploadImage")]
public async Task<HttpResponseMessage> UploadImages([FromForm] IFormFileCollection files)
{
    //files is null, but Request.Form.Files[0] in the immediate window shows the file.
}
public任务上载映像(字符串文件名,内存流)
{
流位置=0;
_基本URL
.AppendPathSegments(“上传图像”)
.PostMultipartAsync(mp=>mp
.AddFile(“文件”、流、文件名))
}
处理请求:

public Task<HttpResponseMessage> UploadImage(string fileName, MemoryStream stream)
{
    stream.Position = 0;

    _baseUrl
        .AppendPathSegments("uploadImage")
        .PostMultipartAsync(mp => mp
            .AddFile("files", stream, fileName))
}
[HttpPost]
[Route("uploadImage")]
public async Task<HttpResponseMessage> UploadImages([FromForm] IFormFileCollection files)
{
    //files is null, but Request.Form.Files[0] in the immediate window shows the file.
}
[HttpPost]
[路由(“上传图像”)]
公共异步任务上载映像([FromForm]IFormFileCollection文件)
{
//files为null,但立即窗口中的Request.Form.files[0]会显示该文件。
}

一个常见的问题似乎是参数的名称与内容处置头中的名称不匹配,但我将它们都更新为
文件
,并且仍然存在相同的问题。

奇怪的是,它在我这方面起作用:

MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream("txt.txt", FileMode.Open, FileAccess.Read))
    file.CopyTo(ms);

ms.Position = 0;
var _baseUrl = "https://localhost:44392/";
var result = await _baseUrl
    .AppendPathSegments("uploadImage")
    .PostMultipartAsync(mp => mp
    .AddFile("files", ms, "txt.txt"));
结果:


请首先尝试使用干净的文件,并使用
等待
来处理请求。

奇怪的是,它在我这边起作用:

MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream("txt.txt", FileMode.Open, FileAccess.Read))
    file.CopyTo(ms);

ms.Position = 0;
var _baseUrl = "https://localhost:44392/";
var result = await _baseUrl
    .AppendPathSegments("uploadImage")
    .PostMultipartAsync(mp => mp
    .AddFile("files", ms, "txt.txt"));
结果:


请首先尝试使用干净的文件,并使用
wait
来处理请求。

我不回答,因为我不知道,但这里有几件事需要尝试:1)删除
[FromForm]
属性,2)因为在本例中您只上载了1个文件,使用
ifformfile
而不是
ifformfilecollection
。我不回答,因为我不知道,但这里有几件事可以尝试:1)删除
[FromForm]
属性,2)因为在本例中您只上载了1个文件,使用
ifformfile
而不是
ifformfilecollection
。我不确定我们的两个环境有什么不同,但我尝试了您的示例,
files
的计数仍然为0,但
Request.Form.files[0]
在即时窗口中显示具有正确文件名和长度的我的文件信息。我不确定这两个环境有什么不同,但我尝试了您的示例,并且
文件的计数仍然为0,但是
Request.Form.files[0]
在即时窗口中显示具有正确文件名和长度的我的文件信息。