FormData 列表数据无法在控制器中发布

问题描述 投票:0回答:0

我正在使用 dot net core 7。这里我有一个上传多个文件的表单 不可能直接使用我有一个不同的类来上传文件和其他数据

注意:这些数据有时不固定,应该是多行 问题仅在于所有其他数据都发布的文件。只有文件没有进入控制器

  public class RegAttachedDto
    {
        public int? DocumentTypeId { get; set; }
        public string? Name { get; set; }
        public IFormFile? RegFIleFile { get; set; }
    }


 public class OrganisationRegDto
    {
        public int RegistrationId { get; set; }
        public string Name { get; set; } = null!;
        public int RegdCategoryId { get; set; }
        public double AuthorizedCapital { get; set; }
        public double PaidupCapital { get; set; }
        public double IssuedCapital { get; set; }
        public bool IsAssociateMember { get; set; }
        public IList<RegAttachedDto> regAttachedDtos { get; set; }
    }

let regAttachedDtosArr = new Array();
let regAttachedDtosfn = () => {
    $('#businessRegDocument  .trdata').each(function (i, row) {
        let DocumentTypeId = $(this).find("td .DocumentTypeId").val();
        let RegFIleFile = $(this).find("td .DocTypeName")[0].files[0];

        let DocDescription = $(this).find("td .DocDescription").val();
        if (RegFIleFile != 'undefined' && DocumentTypeId != '' && DocDescription != '') {
            regAttachedDtosArr.push({
                RegFIleFile: RegFIleFile,
                DocumentTypeId: DocumentTypeId,
                DocDescription: DocDescription

            });
        }
    });
}
let SendToDb = () => {
        regAttachedDtosArr = [];
        regAttachedDtosfn();
        let formdata = new FormData();
        formdata.append("Name", $("#Name").val());
        formdata.append("RegdCategoryId", $("#RegdCategoryId").val());
        formdata.append("AuthorizedCapital", $("#AuthorizedCapital").val());
        formdata.append("PaidupCapital", $("#PaidupCapital").val());
        formdata.append("IssuedCapital", $("#IssuedCapital").val());
        formdata.append("IsAssociateMember", $("#IsAssociateMember").is(':checked'));
        formdata.append("regAttachedDtos", JSON.stringify(regAttachedDtosArr));
        console.log(regAttachedDtosArr); //file are show in this console
        console.log(formdata);
        $.ajax({
            url: '/Controllername/actionName',
            type: 'POST',
            contentType: false,
            processData: false,
            data: formdata
        }).done((res) => {

            toastr.success("Success", {
                timeOut: 8000
            });

        }).catch((ex) => {

            toastr.error(ex);
        });

    }, function () { });
}

$('#regorgForm').submit(function (e) {
    e.preventDefault();
   
        SendToDb();
    
});
  public IActionResult actionName([FromForm]OrganisationRegDto regDto)
        {
            
            var regAttachedFiles = HttpContext.Request.Form["regAttachedDtos"];
            var boardMember = HttpContext.Request.Form["boardMemberDtos"];
            var regAuthority = HttpContext.Request.Form["regAuthorityDtos"];
            var files = HttpContext.Request.Form.Files; // here files are 0
            
            return View("Index");
        }

我曾尝试根据请求类型执行 multipart 并添加和删除 JSON.stringify

即 formdata.append("regAttachedDtos", JSON.stringify(regAttachedDtosArr)); 和 formdata.append("regAttachedDtos", regAttachedDtosArr;

contentType: false,也试试用这个 我也尝试使用这个发送数据

如果我删除 processData: false 没有数据会进入控制器 如果设为真则文件将给出错误

jquery ajax .net-core
© www.soinside.com 2019 - 2024. All rights reserved.