0

My ASP.NET Core 6 app has a form in which users select two files. When the form is submitted I get a 400 error:

Failed to load response data: No resource with given identifier found.

The form submission works fine if only one file is selected.

I would like to know why this is happening and how to fix.

HTML form:

<form id="submitFileUploadForm" asp-page-handler="FileSelected" method="post" enctype="multipart/form-data">
   <input id="selectFileInput" name="SelectedFiles" asp-for="newLayer.SelectedFiles" type="file" multiple>
</form>

Page Model:

public IActionResult OnPostFileSelected(List<IFormFile> SelectedFiles)
{
    //do something with SelectedFiles
}

Model:

public class NewLayer
{
    public IEnumerable<IFormFile>? SelectedFiles { get; set; }
    //various other properties
}

Submit form handler:

//submit form on file select
$("#selectFileInput").change(function () {
    document.getElementById('submitFileUploadForm').submit()
});
NickyLarson
  • 115
  • 1
  • 2
  • 18
  • Single files can be added to the message either in the body or as an attachment. Multiple files will get added only as an attachment. See : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true – jdweng Feb 01 '22 at 18:18

1 Answers1

1

Update

After realising that you had the same issue with the code that I provided, it seems likely it's because of the selected files.

By default ASP.NET Core has a maximum file size upload limit of 30MB, so if you were trying to upload 2x 16MB files, one would work but two wouldn't. You can configure the maximum limit by following Increase upload file size in Asp.Net core


If it helps I made this quick incredibly simple razor page based on the code that you've provided that hopefully you can use to see where you've gone wrong.

Index.cshtml.cs

public class IndexModel : PageModel
{
    public void OnPostFileSelected(IList<IFormFile> SelectedFiles)
    {

    }

    public IEnumerable<IFormFile>? SelectedFiles { get; set; }
}

Index.cshtml

@page
@model IndexModel

<form id="submitFileUploadForm" asp-page-handler="FileSelected" method="post" enctype="multipart/form-data">
    <input id="selectFileInput" name="SelectedFiles" asp-for="SelectedFiles" type="file" multiple>
</form>

@section Scripts {
    <script type="text/javascript">
        $("#selectFileInput").change(function () {
            document.getElementById('submitFileUploadForm').submit()
        });
    </script>
} 
Shoejep
  • 4,414
  • 4
  • 22
  • 26
  • Thanks, I must be blind but I couldn't spot any functional difference between your code and mine? – NickyLarson Feb 02 '22 at 10:19
  • @NickyLarson The only difference I could see is that you're using a separate model for SelectedFiles. Other than that I think the error would be unrelated to code that you've shared i.e. the problem is somewhere else in your project. – Shoejep Feb 02 '22 at 11:21
  • I understand. I've started a new blank project and inserted only the code you posted and still the exact same issue. OnPostFileSelected gets hit if only one file is selected. If there are more I get the same 400 error – NickyLarson Feb 02 '22 at 11:38
  • @NickyLarson Have you tried selecting different files etc.? e.g. there's a 30MB upload limit so if you were trying to upload 2x 16MB files, one would work and two wouldn't. I'm struggling to think of over possible scenarios. – Shoejep Feb 02 '22 at 12:21
  • ah, I did not realise there was such a small upload limit. That was indeed the problem. Two smaller files works just fine! How do we increase this limit? – NickyLarson Feb 02 '22 at 12:55
  • @NickyLarson Updated my answer with a link to another answer – Shoejep Feb 02 '22 at 13:11