0

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?

Buzzy
  • 1,783
  • 18
  • 15
  • does you controller has ` [Route("api/[controller]")]` attribute? Or in a more generic way - how routing is defined? – Set Jul 13 '17 at 14:45

1 Answers1

2

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];
    //...
}
FabioG
  • 2,936
  • 3
  • 31
  • 50