0

I'm struggling my head trying to pass a file and parameters from javascript using AJAX to mvc method. I already successfully send and catch the file, but I don't know have to send and receive the strings parameters.

HTML:

    <div class="buttons-actions" id="buttonsActions" data-import="@Url.Action("ImportClick", "ClientConfiguration")">        
    <input id="importButton" hidden type="file" value="Import" class="buttons-small" />
    <label class="labelImport" for="importButton">Import</label></div>

Javascript:

$('#importButton').on("change", function () {
            var fileUpload = $("#importButton").get(0);
            var files = fileUpload.files;
            
            var systemName = "param1";
            var clientName = "param2";
            
            // Create FormData object  
            var fileData = new FormData();

            // Looping over all files and add it to FormData object  
            for (var i = 0; i < files.length; i++) {
                fileData.append(files[i].name, files[i]);
            }
            
            $.ajax({
            url: ImportClick,
            type: 'POST',
            //here is the thing, how send the parameters and the file
            data: fileData,
            cache: false,
            processData: false,
            contentType: false,
            success: function (response) {
                alert(response);
            },
            error: function (err) {
                alert(err.statusText);
            }
        });

MVC:

[HttpPost]
    public ActionResult ImportClick()
    {
        
        //how take the two parameters??
        
        //Here take the file
        HttpFileCollectionBase files = Request.Files;
        
        byte[] fileData = null;
        
        using (var binaryReader = new BinaryReader(files[0].InputStream))
        {
            fileData = binaryReader.ReadBytes(files[0].ContentLength);
        }
        
        service.ImportAllClientConfigurationData(param1, param2, fileData);
        
    }
  • 1
    https://stackoverflow.com/questions/54396101/how-to-post-file-along-with-form-data-to-mvc-controller-using-ajax does this or this https://stackoverflow.com/questions/20629105/ajax-fileupload-jquery-formdata-in-asp-net-mvc answer your question? – Lucretius May 26 '22 at 21:13

0 Answers0