【问题标题】:400 error when submitting Form with multiple selected files提交带有多个选定文件的表单时出现 400 错误
【发布时间】:2022-02-02 21:11:29
【问题描述】:

我的 ASP.NET Core 6 应用程序有一个表单,用户可以在其中选择两个文件。提交表单时出现 400 错误:

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

如果只选择一个文件,表单提交就可以正常工作。

我想知道为什么会发生这种情况以及如何解决。

HTML 表单:

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

页面模型:

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

型号:

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

提交表单处理程序:

//submit form on file select
$("#selectFileInput").change(function () {
    document.getElementById('submitFileUploadForm').submit()
});

【问题讨论】:

标签: c# html forms asp.net-core razor-pages


【解决方案1】:

更新

在意识到您对我提供的代码有同样的问题后,很可能是因为选择的文件。

默认情况下,ASP.NET Core 的最大文件大小上传限制为 30MB,因此如果您尝试上传 2 个 16MB 的文件,一个可以,但两个不行。您可以通过关注Increase upload file size in Asp.Net core来配置最大限制


如果对我有帮助,我会根据您提供的代码制作这个非常简单的快速剃须刀页面,希望您可以使用它来查看哪里出错了。

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

【讨论】:

  • 谢谢,我一定是瞎了眼,但我没发现你的代码和我的代码在功能上有什么区别?
  • @NickyLarson 我能看到的唯一区别是您为 SelectedFiles 使用了单独的模型。除此之外,我认为该错误与您共享的代码无关,即问题出在您项目的其他地方。
  • 我明白了。我已经开始了一个新的空白项目,只插入了您发布的代码,但仍然是完全相同的问题。如果只选择了一个文件,则 OnPostFileSelected 会被命中。如果还有更多,我会得到相同的 400 错误
  • @NickyLarson 您是否尝试过选择不同的文件等?例如有 30MB 的上传限制,所以如果您尝试上传 2 个 16MB 的文件,一个可以工作,两个不能。我正在努力考虑可能的情况。
  • 啊,没想到上传限制这么小。这确实是问题所在。两个较小的文件就可以了!我们如何提高这个限制?
猜你喜欢
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 2014-10-29
  • 2011-07-28
  • 2014-02-25
相关资源
最近更新 更多