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?

1
  • does you controller has ` [Route("api/[controller]")]` attribute? Or in a more generic way - how routing is defined? Commented Jul 13, 2017 at 14:45

1 Answer 1

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];
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.