Search code examples
asp.net-corekendo-ui-angular2

How to fix ASP.NET Core File Upload empty file


I am trying to upload a csv file from Angular to Asp.NET Core webapi. But getting empty object at the web abi although Fiddler shows something went over the network. I am using Kendo UI to upload the file.

Can you please tell me how to resolve this ? I tried [FromBody] and [FromForm] but no difference.

    [HttpPost]
    [Route(ApiRoutes.EodVariationMargin)]
    public async Task<IActionResult> UploadPlugAsync(object plugs)
    {
        var content = JsonConvert.SerializeObject(plugs); 
        _logger.LogInformation(content);
        return Ok();
    }

Request Count:   1
Bytes Sent:      1,470      (headers:539; body:931)
Bytes Received:  198        (headers:198; body:0)

ACTUAL PERFORMANCE
--------------
ClientConnected:    11:34:36.511
ClientBeginRequest: 11:34:44.561
GotRequestHeaders:  11:34:44.561
ClientDoneRequest:  11:34:44.566
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect: 0ms
HTTPS Handshake:    0ms
ServerConnected:    11:34:36.512
FiddlerBeginRequest:    11:34:44.566
ServerGotRequest:   11:34:44.566
ServerBeginResponse:    11:35:15.629
GotResponseHeaders: 11:35:15.629
ServerDoneResponse: 11:35:15.629
ClientBeginResponse:    11:35:15.629
ClientDoneResponse: 11:35:15.630

    Overall Elapsed:    0:00:31.069

RESPONSE BYTES (by Content-Type)
--------------
~headers~: 198

Solution

  • I am using this and it works:

    [Route("API/files")]
    public class FileController : Controller
    {
        [HttpPost("Upload")]
        public IActionResult SaveUploaded()
        {
            List<string> result = new List<string>();
            var files = this.Request.Form.Files;
            foreach (var file in files)
            {
                using (FileStream fs = System.IO.File.Create("Destination"))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                result.Add(file.FileName);
            }
            return result.ToJson();
        }
    }
    

    And Angular html:

    <kendo-upload saveUrl="API/Files/Upload"
                  ...>
    </kendo-upload>