gpt4 book ai didi

javascript - 将对象和文件从 ajax 非法调用传递到 Web Api 2 Controller

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:22 24 4
gpt4 key购买 nike

                           $.ajax({
url: `/api/issues/${ViewModels.issueViewModel.id}`,
method: "PUT",
data: {dto: ViewModels.issueViewModel, emailAttach: attachFileData},
headers: {
'Authorization': 'Bearer ' + token
}
})

issueViewModel 是一个键值对对象,emailAttach 将是附加到 FormData 的文件webapi2 Controller

 public IHttpActionResult CreateIssue(IssueDto dto, List<HttpFileCollectionBase> emailAttach)
{
//Some logic to handle objects passed
}

IssueDto 看起来像这样

public int Id { get; set; }
public string Description { get; set; }
public string Note { get; set; }

public List<int> PersonId { get; set; } //Extra
public List<string> ApplicationName { get; set; }
public List<string> AttachedFiles { get; set; }

public List<PslApplicationDto> PslApplicationDto { get; set; }
public UrgencyFlag UrgencyFlag { get; set; }
public CompletionFlag CompletionFlag { get; set; }

public DateTime? CompletionDate { get; set; }
public DateTime? DateIssueRegistered { get; set; }
public DateTime? LastUpdate { get; set; }
public int CompletionDuration { get; set; }

public SolutionDto SolutionDto { get; set; }
public PsUserDto PsUser{ get; set; }
public EmailDto EmailDto { get; set; }
public IssueCategorieDto Category { get; set; }
public List<PersonDto> PersonDtos { get; set; }
public List<IssueTagsDto> IssueTags { get; set; }
public List<int> IssueTagsId { get; set; } // Extra info

public int CategorieId { get; set; }
public int PsUserId { get; set; }

所有这些信息都绑定(bind)到 ViewModel 并传递给 Controller emailAttach 是从这段代码生成的键值 FormData

function attachFile(e) {
e.stopImmediatePropagation();
e.preventDefault();

var attachmentInput = document.getElementById("attachInput");

if (typeof (attachmentInput.files[0]) === "undefined") {
return toastr.error("Please select a file to attach");
}


attachFileData.append(attachmentInput.files[0].name, attachmentInput.files[0]);
appendAttachmentToForm(attachmentInput.files[0]);

最佳答案

尝试将IssueDto类中dto和文件的数据类型改为string。

关于javascript - 将对象和文件从 ajax 非法调用传递到 Web Api 2 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com