Asp.Net Core 2.0中的'HttpPostedFileBase' [英] 'HttpPostedFileBase' in Asp.Net Core 2.0

查看:1844
本文介绍了Asp.Net Core 2.0中的'HttpPostedFileBase'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在开发一个ReactAPI应用,该应用正在调用API(由.NET Core 2.0开发).

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/zh-cn/aspnet/core/mvc/models/file-uploads?view = aspnetcore-2.1

IFormFile在以下命名空间Microsoft.AspNetCore.Http中.

这篇关于Asp.Net Core 2.0中的'HttpPostedFileBase'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆