ng2-file-upload doesn't trigger the controller

2019-08-17 01:21发布

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.

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();
     }

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?

1条回答
迷人小祖宗
2楼-- · 2019-08-17 01:48

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];
    //...
}
查看更多
登录 后发表回答