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

标签 asp.net-web-api asp.net-core swagger asp.net-core-webapi

我正在尝试使用 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/

相关文章:

c# - Web API 不会将 bool 返回类型序列化为 json boolean 值

asp.net - WCF 和 ASP.NET Web API : Benefits of both?

c# - 使用 NUnit 模拟 EF 的 ExecuteSqlCommand

java - Swagger2 更改 Swagger Ui 的基本路径

c# - 异步任务 <HttpResponseMessage> 获取 VS HttpResponseMessage 获取

c# - 单核机器上的 Web API 和 Async/Await 优势

entity-framework-6 - 带有 EF6 的 ASP.NET-5

c# - 检测到 Azure 网站环境; key 不会静态加密 - Dot Net Core 2.1 - Azure Web App

yaml - 如何在swagger属性中添加多个示例值?

asp.net-core - .Net Core 2.0 中的 SwashBuckle UI 显示错误