You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC多上传控件场景下无法向服务器提交数据问题

大文件上传处理失败问题

我使用Telerik控件构建了如下表单用于向服务器提交数据,但当上传文件达到一定大小后,服务器无法正常处理该请求。我已在配置文件中添加了请求大小限制相关设置,但问题仍未解决。

前端视图代码(Razor + JavaScript)

@using (Html.BeginForm("Add", "Storage", FormMethod.Post, new { @id = "frmItem", @class = "form", enctype = "multipart/form-data" }))
{
<table class="table table-bordered">
    <thead class="bg-dark text-center text-white">
        <tr>
            <th colspan="2">Add/Modify Item</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style="width:404px;">Item ID Name</td>
            <td>
                @(Html.Kendo().TextBox().Name("ItemName")
        .HtmlAttributes(new { placeholder = "Item ID Name", required = "required", validationmessage = "Enter Item ID Name" }))
            </td>
        </tr>
        <tr>
            <td>UN Number</td>
            <td>
                @(Html.Kendo().TextBox().Name("UnNumber")
        .HtmlAttributes(new { placeholder = "UN Number", required = "required", validationmessage = "Enter UN Number" }))
            </td>
        </tr>
        <tr>
            <td>Packing Instructions</td>
            <td>
                @(Html.Kendo().TextBox().Name("PackingInstructions")
        .HtmlAttributes(new { placeholder = "Packing Instructions", required = "required", validationmessage = "Enter Packing Instructions" }))
            </td>
        </tr>
        <tr>
            <td>SDS Sheet</td>
            <td>
                @(Html.Kendo().Upload().Name("SdsSheet")
        .Multiple(false))
            </td>
        </tr>
        <tr>
            <td>Cargo Only?</td>
            <td>@(Html.Kendo().CheckBox().Name("Cargo"))</td>
        </tr>
        <tr>
            <td>Example Shipment Ground (Fully Regulated)</td>
            <td>@(Html.Kendo().Upload().Name("GroundShipment"))</td>
        </tr>
        <tr>
            <td>Example Shipment Next Day Air (Fully Regulated)</td>
            <td>@(Html.Kendo().Upload().Name("NextDayShipment"))</td>
        </tr>
        <tr>
            <td>Example Shipment Freight (Fully Regulated)</td>
            <td>@(Html.Kendo().Upload().Name("FreightShipment"))</td>
        </tr>
        <tr>
            <td>Example Shipment Ground (Limited Quantity)</td>
            <td>@(Html.Kendo().Upload().Name("GroundShipmentLimited"))</td>
        </tr>
        <tr>
            <td>Example Shipment Next Day Air (Limited Quantity)</td>
            <td>@(Html.Kendo().Upload().Name("NextDayShipmentLimited"))</td>
        </tr>
        <tr>
            <td>Example Shipment Freight (Limited Quantity)</td>
            <td>@(Html.Kendo().Upload().Name("FreightShipmentLimited"))</td>
        </tr>
        <tr>
            <td colspan="2">
                <button id="btnSubmit" class="k-button k-button-solid-primary k-button-solid k-button-md k-rounded-md" type="button">Submit</button>
                @Html.ActionLink("Download", "GetReport", new { controller = "Storage" }, new { target = "_blank" })
            </td>
        </tr>
    </tbody>
</table>
}
 <script>
$(function () {
    $("#btnSubmit").on("click", function () {
       var validator = $("#frmItem").kendoValidator().data("kendoValidator");
        if (validator.validate()) {
            var formdata = new FormData($('#frmItem').get(0));
            showSpinner();
            let url = "/Storage/Add/";
            $.ajax({
                url: url,
                type: 'POST',
                data: formdata,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (result) {
                    if (result === "OK") {
                        hideSpinner();
                        swal(
                            'Item submitted',
                            'Item saved successfully',
                            'success',
                            5000
                        ).then(function () {
                            window.location.reload();
                        });
                    }
                    else {
                        swal(
                            'Error',
                            'An internal error occurred. Please retry',
                            'error'
                        ).then(function () {
                        });
                        hideSpinner();
                    }
                }
            });
        }
    });
});
</script>

控制器处理方法

[HttpPost]
public JsonResult Add(HazMatItemModel itemModel)
{
    ItemRepository itemRepository = new ItemRepository();
    itemModel.FolderPath = "~Uploads/";

    if (itemRepository.AddUpdateItem(itemModel))
       return new JsonResult { Data = "OK" };

    return new JsonResult { Data = "Error" };
 }

Web.config配置设置

<system.web>
    <httpRuntime targetFramework="4.7.2" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483647" />
            <!--50MB-->
        </requestFiltering>
    </security>
</system.webServer>

模型类代码

using System.ComponentModel.DataAnnotations;
using System.Web;

namespace HazMat.Models
{
public class HazMatItemModel : BaseEntity
{
    public int ItemId { get; set; }
    [Required]
    public string ItemName { get; set; }
    [Required]
    public string UnNumber { get; set; }
    [Required]
    public string PackingInstructions { get; set; }
    public string FolderPath { get; set; }
    public HttpPostedFileBase SdsSheet { get; set; }
    public string SdsSheetFileName { get; set; }
    public bool IsCargo { get; set; }
    public HttpPostedFileBase GroundShipment { get; set; }
    public string GroundShipmentFileName { get; set; }
    public HttpPostedFileBase NextDayShipment { get; set; }
    public string NextDayShipmentFileName { get; set; }
    public HttpPostedFileBase FreightShipment { get; set; }
    public string FreightShipmentFileName { get; set; }
    public HttpPostedFileBase GroundShipmentLimited { get; set; }
    public string GroundShipmentLimitedFileName { get; set; }
    public HttpPostedFileBase NextDayShipmentLimited { get; set; }
    public string NextDayShipmentLimitedFileName { get; set; }
    public HttpPostedFileBase FreightShipmentLimited { get; set; }
    public string FreightShipmentLimitedFileName { get; set; }
  }
}

解决方案排查步骤

  • 确认配置生效范围:检查web.config中的设置是否放在正确位置,若应用有子目录或虚拟目录,需确保配置覆盖目标站点;注意maxAllowedContentLength单位是字节,当前配置为2GB,与注释的50MB不符,可调整为52428800(50MB)匹配需求。

  • 检查模型绑定与文件处理逻辑:验证ItemRepository.AddUpdateItem方法是否存在文件大小限制,或处理文件时是否抛出IO异常(如磁盘空间不足、权限不足)。可在控制器中添加日志,输出itemModel中各文件属性的状态,确认文件是否被正确接收。

  • 调整IIS站点级配置:打开IIS管理器,进入目标站点的“请求筛选”功能,检查“最大允许内容长度”是否与web.config配置一致,避免站点级配置覆盖全局设置。

  • 增加AJAX请求超时:大文件上传耗时较长,需在AJAX请求中添加超时设置,避免请求因超时中断:

    $.ajax({
        // 原有配置
        timeout: 300000 // 设置为5分钟,根据实际情况调整
    });
    
  • 检查应用程序池设置:确认IIS应用程序池的“队列长度”足够大,关闭不必要的自动回收规则,避免大文件上传过程中应用池被回收。

  • 启用详细错误日志:在web.config中开启详细错误输出,捕获具体异常信息:

    <system.web>
        <customErrors mode="Off"/>
    </system.web>
    <system.webServer>
        <httpErrors errorMode="Detailed"/>
    </system.webServer>
    

内容的提问来源于stack exchange,提问作者Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:22:10