我正在尝试使用 IFormfile 和其他属性模型数据在 asp.net core web api 中使用 swagger 发布文件,但我有任何方法可以同时执行这两项操作(上传文件和其他模型属性)。
请建议我一些好方法来做到这一点。
最佳答案
您可以创建一个具有字符串和 IFormFile 属性的模型并使用 [FromForm] 属性:
public class UploadModel{
public string Var1 { get; set; }
public IFormFile File { get; set; }
}
在您的 Controller 中:
[HttpPost("/upload")]
public async Task Upload([FromForm] UploadModel model)
{
if (model.File == null) throw new Exception("File is null");
if (model.File.Length == 0) throw new Exception("File is empty");
model.Var1 += "hello world";
using (Stream stream = file.OpenReadStream())
{
using (var binaryReader = new BinaryReader(stream))
{
// Save the file here.
}
}
}
关于asp.net-web-api - 如何在asp.net core webapi中上传带有其他模型属性的Iformfile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260353/