C# API POST调用上载包含文件的对象列表
我正在使用ASP.NET Core 2.2中的C# API POST调用上载包含文件的对象列表,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我正在使用ASP.NET Core 2.2中的ifformfile构建一个文件上传web服务调用。因为我需要与每个文件上传相关联的特定数据,所以我创建了一个自定义模型类来包含带有ifformfile属性的数据 public class Document { public string FileId { get; set; } public string FileTitle { get; set; } public string CategoryId { get; set;
ifformfile
构建一个文件上传web服务调用。因为我需要与每个文件上传相关联的特定数据,所以我创建了一个自定义模型类来包含带有ifformfile
属性的数据
public class Document
{
public string FileId { get; set; }
public string FileTitle { get; set; }
public string CategoryId { get; set; }
public IFormFile Content { get; set; }
}
当只上载一个文档时,下面的方法可以正常工作
[HttpPost]
[Route("UploadDoc")]
public async Task<IActionResult> DocumentUpload ([FromForm] Document document) { }
[HttpPost]
[路线(“上传文件”)]
公共异步任务文档上载([FromForm]文档文档){}
然而,我希望接受一份文件清单,以便立即上传。当我将帖子配置为接受列表时,它将不再工作。我知道IList
可以工作,但问题是我需要每个文件的额外数据
[HttpPost]
[Route("UploadDoc")]
public async Task<IActionResult> DocumentUpload ([FromForm] IList<Document> document) { }
[HttpPost]
[路线(“上传文件”)]
公共异步任务文档上载([FromForm]IList文档){}
我正在使用Postman测试我的API,我已经包括了一个表单数据调用的屏幕截图。当我被处决时,我的邮递员将被绞死。奇怪的是,当我从文档类中删除IFormFile属性时,调用就起作用了。我能做到这一点吗?或者有解决办法吗
要将列表对象从postman传递到api,可以尝试使用类似结构的
参数名[index].fieldName
这是一个对你有用的演示
为什么您不能尝试IFormFile的具体实现呢。并将该具体类型用作列表参数
public class Document : IFormFile
{
public string ContentType => throw new NotImplementedException();
public string ContentDisposition => throw new NotImplementedException();
public IHeaderDictionary Headers => throw new NotImplementedException();
public long Length => throw new NotImplementedException();
public string Name => throw new NotImplementedException();
public string FileName => throw new NotImplementedException();
public void CopyTo(Stream target)
{
throw new NotImplementedException();
}
public Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
public Stream OpenReadStream()
{
throw new NotImplementedException();
}
}
您还可以通过添加所需的属性来自定义类。
值得一试。试一试,让我知道你是怎么做的。这实际上是,但有一个解决办法
将项目的索引包括到表单帖子中,如下所示:
Html表单:
<input type="hidden" name="document.Index" value="0" />
<input type="file" name="document.Content[0]" />
<input type="hidden" name="document.Index" value="1" />
<input type="file" name="document.Content[1]" />
key value
document.Index 0
document.Content[0] (binary)
document.Index 1
document.Content[1] (binary)
我已经创建了一个并包含了。这是一个已知的
作为一种解决方法,您可以在另一个类中包装ifformfile
,这样就可以了
public class Company
{
public IList<Person> Employees { get; set; } = new List<Person>();
}
public class Person
{
public FormFileWrapper IdImage { get; set; }
}
public class FormFileWrapper
{
public IFormFile File { get; set; }
}
上市公司
{
公共IList雇员{get;set;}=new List();
}
公共阶层人士
{
public FormFileWrapper IdImage{get;set;}
}
公共类FormFileWrapper
{
公共文件{get;set;}
}
然后,您不必在html中添加索引
<form action="http://localhost:5000/api/Values" method="post" enctype="multipart/form-data">
<p><input type="file" name="Employees[0].IdImage.File"></p>
<p><input type="file" name="Employees[1].IdImage.File"></p>
<p><button type="submit">Submit</button></p>
</form>
提交
不确定IList是否有效,但您可以尝试将base64编码的文件内容作为字符串传递。谢谢您的建议。我刚试过这个,但我认为点符号不起作用。数据没有通过此服务器传递way@jmak24这是我的测试,做一个测试并分享差异。@TaoZhou如果你在嵌套的导航集合中有IFormFile,它将不起作用,直到现在它还没有修复错误,这个解决方法将起作用。但这有点危险,因为您将允许API使用者在不传递索引字段的情况下向您发送请求,这将使请求进入无限循环,并可能导致API下降。