IFormFile is always empty in Asp.Net Core WebAPI IFormFile is always empty in Asp.Net Core WebAPI asp.net asp.net

IFormFile is always empty in Asp.Net Core WebAPI


IFormFile will only work if you input name is the same as your method parameter name. In your case the input name is 'files' and the method parameter name is 'file'. Make them the same and it should work.


This is how to do it with angularjs:

vm.addFile = function () {                                      var fileUpload = $("#file").get(0);                var files = fileUpload.files;                var data = new FormData();                for (var i = 0; i < files.length ; i++) {                    data.append(files[i].name, files[i]);                }                $http.post("/api/Files/", data, {                    headers: { 'Content-Type': undefined },                    transformRequest: angular.identity                }).success(function (data, status, headers, config) {                }).error(function (data, status, headers, config) {                });}

And in web Api:

[HttpPost]public async Task<IActionResult> PostFile(){ //Read all files from angularjs FormData post request var files = Request.Form.Files; var strigValue = Request.Form.Keys; .....}

Or like this:

    [HttpPost]    public async Task<IActionResult>  PostFiles(IFormCollection collection)    {        var f = collection.Files;                                     foreach (var file in f)            {                //....             }               }


You can do it also with kendo upload much simpler:

$("#files").kendoUpload({        async: {            saveUrl: dataService.upload,            removeUrl: dataService.remove,            autoUpload: false                                                    },        success: onSuccess,        files: files    });