Anand
Anand

Reputation: 1691

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

Upvotes: 1

Views: 1184

Answers (2)

Makla
Makla

Reputation: 10459

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>

Upvotes: 2

Chris Pratt
Chris Pratt

Reputation: 239290

You cannot bind to an action param of type object. Rather, I suppose you can but it won't do anything for you because object has no properties. Hence, the modelbinder will simply discard everything that's posted.

If you're trying to accept an upload in that param, then it should be of type IFormFile. If you're trying to accept a JSON payload, then it should be an actual class that you create to represent that payload. For example, if you were sending JSON like:

{
    "foo": "bar"
}

Then, you need a class like:

public class MyDTO
{
    public string Foo { get; set; }
}

If you're trying to send an upload as part of your JSON payload, then your DTO class should have a byte[] property named after the key of the file upload, and your upload data should be sent as Base 64. For example:

{
    // other stuff
    "file": "{base64 encoded byte array}"
}

With a DTO like:

public class MyDTO
{
    // other properties
    public byte[] File { get; set; }
}

Upvotes: 1

Related Questions