.NET Core 中的 IFormFile 与 byte[] 以及文件上传最佳实践

问题描述 投票:0回答:1

我正在使用 .NET CORE 设计 REST API 控制器,以便我可以上传文件,我应该在模型请求中使用 IFormFile 还是 byte[] ?

It's better to do this ?

public class AttachmentModel
    {
        public string FileName { get; set; }
        public byte[] FileContent { get; set; }
   }

or this ?

public class AttachmentModel
    {
        public string FileName { get; set; }
        public IFormFile  FileContent { get; set; }
   }
asp.net-core .net-core file-upload rest iformfile
1个回答
0
投票

https://learn.microsoft.com/it-it/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-8.0

IFormFile 包含一些 byte[] 无法提供的有用信息。

这在上下文中特别方便(例如:使用 Blazor 的 html 输入文件),其中所有这些信息均由浏览器提供,无需编写代码。

IFormFile 的结构是映射多部分表单内容类型的文件,所以我认为这是在这些情况下的最佳选择。

© www.soinside.com 2019 - 2024. All rights reserved.