gpt4 book ai didi

c# - ASP .Net MVC Filepond 在 Controller 中获取文件唯一 ID

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

我正在阅读 Filepond 的文档 enter link description here

它们提供了处理文件上传时我应该遵循的基本步骤。我已经解决了 1-4 个(希望是正确的)

enter image description here

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "login-form-w", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
@Html.HiddenFor(m => m.ReturnUrl)
<input type="file"
class="filepond"
name="filepond"
multiple
data-max-file-size="3MB"
data-max-files="3">
<div class="form-label-group">
<label for="inputFirstName">First Name</label>
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", placeholder = "Your First Name", @type = "text", @id = "inputFirstName", required = "required" })
</div>
<div class="form-label-group">
<label for="inputLastName">Last Name</label>
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control", placeholder = "Your Last Name", @type = "text", @id = "inputLastName", required = "required" })
</div>
<button class="btn btn-lg btn-block wd-btn-round-2 text-uppercase font-weight-bold mb-2 btn-outline-black bg-color-yellow" type="submit">Register</button>
}

我的问题是如何使用 post 方法提交隐藏文件?

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
.... code for registering
}

RegisterViewModel.cs

    [Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }

现在我的问题是我应该向 RegisterViewModel 添加什么,以便它拾取隐藏的输入字段(可以是多个)。

enter image description here

最佳答案

我会用一个使用隐藏文本字段的简单解决方案来完成。

将以下内容添加到您的表单

HTML 字段

<input id="FileIDs" type="hidden" asp-for="FileIDs" /> 

模型属性

[BindProperty]
public string FileIDs { get; set; }

Filepond 加载事件

pond.setOptions({
server: {
process: {
method: 'POST',
onload: (response) => {
var val = $("#FileIDs").val();
(val == '') ? $("#FileIDs").val(response) : $("#FileIDs").val(val + "," + response);
}
}
}
});

上传后,ids 将被放置在文本字段FileIDs

<input id="FileIDs" type="hidden" name="FileIDs" value="id1,id2,...">

当然,上传成功后你的服务器应该返回id。

表单提交后,您将在模型的 FileIDs 属性中获得 ID。
您只需要将它们拆分为一个文本列表。

var list = FileIDs.Split(",");

关于c# - ASP .Net MVC Filepond 在 Controller 中获取文件唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57984458/

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