Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

75
Visualizações
How to submit files to MVC Action using FormData

I am creating an ASP.NET Core 6 file upload application. This is my code:

FORM

<form asp-controller="Home" asp-action="UploadFiles" method="post" enctype="multipart/form-data" id="uploadForm">
    <input type="file" class="form-control-file" multiple name="files">
    @foreach (var folder in @Model.Folders)
    {
        <input id="filePathUrl" type="radio" asp-for="Folders" value="@folder" />
        @folder
    }
<button type="submit" class="btn custom-button">Upload Files</button>

jQuery

    function processFiles(confirmOverwrite = false){
    const filePath = $("#filePathUrl").val();
    const fileList = $(".form-control-file")[0].files;
    const files = Array.from(fileList);

    let formData = new FormData();
    files.forEach(file => formData.append("files[]", file));

    const URL = '@Url.Action("UploadFiles", "Home")';

    $.ajax({
        url: URL + '?folders=' + filePath + '&confirmOverwrite=' + confirmOverwrite,
        type: 'post',
        data: formData,
        // dataType: 'json',
        contentType: false,
        processData: false,
              ...

MVC Action

       [HttpPost]
    public async Task<JsonResult> UploadFiles(IEnumerable<IFormFile> files, string folders, bool confirmOverwrite = false)
    {
        List<string> existingFiles = new();
        var filesToBeSaved = new List<(IFormFile file, string filePath)>();
        
        foreach (var file in files)
        {
            string trimmedFileName = file.FileName.Replace(" ", "");
            var filePath = Path.Combine(folders, trimmedFileName);

            if (System.IO.File.Exists(filePath) && !confirmOverwrite)
            {
                existingFiles.Add(file.FileName + "(" + trimmedFileName + ")");
            }
            else
            {
                filesToBeSaved.Add((file, filePath));
            }
        }
       ...

I have removed alot of code to keep things relevant and simple.

Why is files argument appearing as null when I submit the form??

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Model Binding binds the parameter by name. Your backend here IEnumerable<IFormFile> files name is files .

So you need to change your code as shown below:

 files.forEach(file => formData.append("files", file));

Also, I know you may want to post it by array index. For complex list model type only, you may need a post like: model[index].PropertyName . Check here .

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda