为什么文件IFormFile = null? [英] Why file IFormFile = null?

查看:179
本文介绍了为什么文件IFormFile = null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Route("api/file")]
[Produces("application/json")]
[Consumes("application/json", "application/json-patch+json", "multipart/form-data")]
[ApiController]
public class FileController : ControllerBase
{
    public FileController()
    {
    }

    [HttpPost]
    public async Task<IActionResult> PostProfilePicture([FromQuery]IFormFile file)
    {
        var stream = file.OpenReadStream();
        var name = file.FileName;
        return null;
    }
}

邮递员

调试

最后的文件= null如何解决这个问题?

In the end file = null How to solve this problem?

推荐答案

我想您是从 IFormFile 中获取空值的,因为您是在Controller类而不是控制器上为此操作指定了必需的属性方法.如下更新代码即可解决问题.

I guess you're getting null from IFormFile because you specify required attributes for this operation on the Controller class, not on the controller method. Updating your code as below will solve the problem.

[Route("api/file")]
[ApiController]
public class FileController : ControllerBase
{

    public FileController()
    {

    }

    [HttpPost]
    [Produces("application/json")]
    [Consumes("multipart/form-data")]
    public async Task<IActionResult> PostProfilePicture([FromForm]IFormFile file)
    {
        var stream = file.OpenReadStream();
        var name = file.FileName;
        return null;
    }
}

希望这可以解决您的问题.

Hope this solves your problem.

这篇关于为什么文件IFormFile = null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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