Search code examples
jqueryajaxasp.net-corekendo-uikendo-upload

Kendo upload using Ajax Post to upload files in MVC core


I have used kendo upload to upload files using Ajax Post in MVC core. I can call the API url but data is coming null. Have i passed data correctly? Here is my code.

<form id="uploadForm" action='@Url.Action("UploadFile", new { RmclientAccid = "1"  , AsAtDate = "testdate"})' method="post">
        @(Html.Kendo().Upload()
                        .HtmlAttributes(new { aria_label = "files" })
                        .Name("fileUpload")
        )
        <p style="padding-top: 1em; text-align: right">
            <button type="submit" class="k-button k-primary">Submit</button>
        </p>
    </form>

$(function () {
            $("#uploadForm").submit(function (e) {
                e.preventDefault();
                var upload = $("#fileUpload").data("kendoUpload"),
                    files = upload.getFiles(),
                    myFile = files[0];
                console.log(files);
                $.ajax({
                    type: 'POST',
                    url: '/RmReportTasks/UploadFile/',
                    dataType: 'json', 
                    processData: false,
                    contentType: false,
                    data: {
                        fileUpload: files[0].rawFile,
                        RmclientAccid: "1",
                        AsAtDate: "testdate"
                    },
                    success: function (data) {
                        console.log("succcess")
                    },
                    error: function (err) {
                        console.log("failure");
                    }
                });
            });
    });

Here is my controller.

 [HttpPost]
        public async Task<JsonResult> UploadFile(IEnumerable<IFormFile> fileUpload , string RmclientAccid, string AsAtDate)
        {
            var result = await _fileUploadManager.UploadDocument(fileUpload, RmclientAccid, AsAtDate);
            return Json(result);
        }

Solution

  • You can not send files directly using ajax to the server.

    $(function () {
                $("#uploadForm").submit(function (e) {
                    e.preventDefault();
                    var formData = new FormData();
                    var totalFiles = document.getElementById("uploadForm").files.length;
                    for (var i = 0; i < totalFiles; i++) {
                       var file = document.getElementById("uploadForm").files[i];
                       formData.append("uploadForm", file);
                    }
                    $.ajax({
                        type: 'POST',
                        url: '/RmReportTasks/UploadFile/',
                        dataType: 'json', 
                        processData: false,
                        contentType: false,
                        data: formData,
                        success: function (data) {
                            console.log("succcess")
                        },
                        error: function (err) {
                            console.log("failure");
                        }
                    });
                });
        });
    

    And on the server-side, you can receive the file like this.

        [HttpPost]
        public void UploadFile()
        { 
           for (int i = 0; i < Request.Files.Count; i++)
                {
                    var file = Request.Files[i];
    
                    var fileName = Path.GetFileName(file.FileName);
    
                    // Write logic to handle file here.
                }
       }