3

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??

3
  • What if you change to files.forEach(file => formData.append("files", file)); I didn't see you invoke processFiles function in your code, how do you invoke it? Commented Mar 15, 2022 at 5:22
  • @Rena Thank you so much! That did the trick. I've spent HOURS on this. Do you have an explanation for why this works? Thank you! Commented Mar 15, 2022 at 5:26
  • Hi @urizark, updated with explanation. pls check. Commented Mar 15, 2022 at 5:42

1 Answer 1

1

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

So you need change your code like below:

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

Besides, I know you may want to post it by array index. Only for complex list model type, you may need post like: model[index].PropertyName. Refer to here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.