ng2-file-upload doesn't trigger the controller method .net core web api

不问归期 提交于 2019-12-25 09:13:43

问题


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:


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];
    //...
}


来源:https://stackoverflow.com/questions/45081395/ng2-file-upload-doesnt-trigger-the-controller-method-net-core-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!