ng2-file-upload不会触发控制器方法.NET Core Web API [英] ng2-file-upload doesn't trigger the controller method .net core web api

查看:53
本文介绍了ng2-file-upload不会触发控制器方法.NET Core Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Content-Type设置为"multipart/form-data"时,上载方法不会触发控制器方法.如果Content-Type设置为'application/json'方法将被触发,但文件对象为null.

The upload method doesn't trigger a controller method when Content-Type set to 'multipart/form-data'. If Content-Type set to 'application/json' method will triggered but file object will be null.

这是我的客户端(角度4)实现:

Here is my client(angular 4) implementation:

    public uploader = new FileUploader({
        url: "/api/mycontroller/uploadfile",
        allowedFileType: ["pdf"],
        headers: <Headers[]>[
            //{ name: 'Content-Type', value: 'application/json' }
            { name: 'Content-Type', value: 'multipart/form-data' }
        ]
    });
    
     save() {
        //...
        this.uploader.queue.reverse()[0].upload();
     }

这是我的Web API控制器(.net核心1.0):

It is my web api controller(.net core 1.0):

[Route("uploadfile")]
[HttpPost]
public async Task<ActionResult> UploadFile([FromBody]IFormFile file)
{
   //...
}

也许我忘记添加一些特殊参数或其他内容了?

Maybe I forgot to add some special parameters or something else?

推荐答案

我相信您的Controller方法应该是无参数的,并且您应该通过HttpContext获取文件,但是我现在没有办法对其进行测试,所以我不能确定...但是我认为应该是这样的:

I believe your Controller Method should be parameterless and you should get your file through HttpContext, I don't have a way to test it right now though, so I can't be sure... but I think it should be something like:

[Route("uploadfile")]
[HttpPost]
public async Task<ActionResult> UploadFile()
{
    var file = HttpContext.Current.Request.Files[0];
    //...
}

这篇关于ng2-file-upload不会触发控制器方法.NET Core Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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