如何在asp.net core webapi中上传带有其他模型属性的Iformfile?

Kap*_*arg 5 asp.net-web-api swagger asp.net-core asp.net-core-webapi

我正在尝试使用 swagger 在 asp.net core web api 中使用 IFormfile 和其他属性模型数据发布文件,但我有办法同时完成这两件事(上传文件和其他模型属性)。

请建议我一些好方法来做到这一点。

cod*_*ter 1

可能如下...

[HttpPost("/upload/{var1}")]
public async Task Upload(string var1, IFormFile file)
{
    if (file == null) throw new Exception("File is null");
    if (file.Length == 0) throw new Exception("File is empty");

    using (Stream stream = file.OpenReadStream())
    {
        using (var binaryReader = new BinaryReader(stream))
        {
           // Save the file here.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)