#39;HttpPostedFileBase#39; in Asp.Net Core 2.0(Asp.Net Core 2.0 中的“HttpPostedFileBase)
问题描述
我最近正在开发一个调用 API(使用 .NET Core 2.0 开发)的 ReactJS 应用程序.
I'm recently working on a ReactJS app that's calling an API (developed with .NET Core 2.0).
我的问题是如何在 .NET Core 2.0 API 中使用 HttpPostedFileBase
来获取文件内容并将其保存在数据库中.
My question is how to use HttpPostedFileBase
in an .NET Core 2.0 API in order to get file content and save it in database.
推荐答案
你在 ASP.NET Core 2.0 中没有 HttpPostedFileBase
,但是你可以使用 IFormFile
.
You don't have HttpPostedFileBase
in ASP.NET Core 2.0, but you can use IFormFile
.
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
// full path to file in temp location
var filePath = Path.GetTempFileName();
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
// process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok(new { count = files.Count, size, filePath});
}
更多:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1
IFormFile
位于以下命名空间 Microsoft.AspNetCore.Http
.
IFormFile
is in the following namespace Microsoft.AspNetCore.Http
.
这篇关于Asp.Net Core 2.0 中的“HttpPostedFileBase"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Asp.Net Core 2.0 中的“HttpPostedFileBase"


基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01