Search code examples
javascriptjqueryasp.net-coreform-data

Api from Asp core controller not receive all the value that FormData.append(key,value) appended


I want to upload files(image, setup file) and some other text string together. This is my ajax code:

function uploadFiles(inputId) {

var input = document.getElementById(inputId);
var files = input.files;
formData = new FormData();

for (var i = 0; i !== files.length; i++) {
    var temp1 = files[i];
}

formData.append('temp', 1);
formData.append('temp', "ksjhdfksdjf");

$.ajax({
    type: "POST",
    url: "/Admin/FileUploadView/SaveEntity",
    data: formData,
    contentType: false,
    processData: false,

    beforeSend: function () {
        tedu.startLoading();
    },
    success: function () {

    },
    error: function () {

    }
});
}

This is my controller:

enter image description here

When I log all the files that formData object contain, everything is ok:

enter image description here

But I only receive 2 image's objects in the controller:

enter image description here

Now I want that I can receive all the files that I appended. Do you have any ideas, please help me.


Solution

  • You do not receive your temp in your controller.

    js:

    function uploadFiles(inputId) {
        var input = document.getElementById(inputId);
        var files = input.files;
        var formData = new FormData();
    
        for (var i = 0; i != files.length; i++) {
            formData.append("files", files[i]);
        }
        formData.append('temp', 1);
        formData.append('temp', "ksjhdfksdjf");
       //ajax
    

    controller:

    [HttpPost]
    public async Task<IActionResult> SaveEntity(IList<IFormFile> files,List<string> temp)