【问题标题】:How to upload file with model with ASP.NET Core 1.0?如何使用 ASP.NET Core 1.0 上传带有模型的文件?
【发布时间】:2019-11-06 05:26:30
【问题描述】:

我正在尝试使用 ASP.NET Core 1.0 制作一个小的上传文件 api。 我有一个从 MVC5 应用程序中获取的模型:

public class RoomModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public HttpPostedFileBase Image { get; set; }
}

我想做一个类似的函数:

[HttpPost("upload")]
public IActionResult Upload(List<RoomModel> rooms)
{
       // Check and upload file here.
       // Save parameter to database.
}

在我的 MVC5 中,使用 HttpPostedFileBase 是可以的,但在 ASP.NET Core 中,我不知道如何使用相同的结果存档。

谁能帮帮我? 谢谢。

P/S:我搜索过类似的教程,但一无所获。我读过的每个教程都只是关于获取键值参数,没有教程是关于在这样的模型中放置信息的。

【问题讨论】:

    标签: asp.net-core-1.0


    【解决方案1】:

    终于找到了这篇文章:

    http://www.mikesdotnetting.com/article/288/uploading-files-with-asp-net-core-1-0-mvc

    从这篇文章中,我的 RoomModel 将是这样的:

    public class RoomModel
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    
        public IFormFile Image { get; set; }
    }
    

    所以,据我所知,IFromFile 替换了 HttpPostedFileBase,处理文件的步骤与 HttpPostedFileBase 相同。

    【讨论】:

    • 该答案适用于 .NET Core,但您的问题是关于 Web Api 2,即 .NET Framework。这不应该是正确的答案。
    • 糟糕,抱歉。我的标题不正确:(。当时我在问asp.net core
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2017-10-30
    • 2021-09-07
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多