#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"
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
