gpt4 book ai didi

asp.net-mvc - MVC : How to know which input value from HttpPostedFileBase

转载 作者:行者123 更新时间:2023-12-03 23:56:44 24 4
gpt4 key购买 nike

让我先尽可能简单地解释一下我的情况:

假设我有一个包含多个 [input type='File'] 的页面。其中一些可能会被选择为文件,而另一些则不会。

在我的 httppost 方法中,我知道我需要使用像“IEnumerable files”这样的参数来获取文件名,而且每个 [input] 名称我都应该定义 = 'files' 或 'files[0]','files[ 1]'等.....

我的问题是:在获取HttpPostedFileBase 列表时,如何确定哪个文件属于哪个输入控件?因为有些输入可能会留空。

另外,因为这些 [input] 是动态创建的,没有固定数量,我无法在 httppost 方法中为每个输入硬编码参数。

最佳答案

试试这个解决方案:

查看:

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file1" />
<input type="file" name="file2"/>
<input type="file" name="file3"/>
<input type="file" name="file4"/>
<input type="file" name="file5"/>

<input type="submit" value="go" />
}

Controller :

  var uploaded = Request.Files.AllKeys
.Select(x => new {file = Request.Files[x], name = x})
.Where(x => x.file.ContentLength > 0).ToList();

“上传的”anonymouse 类型将包含属于输入控件名称的文件,并且将只包含选择的输入名称

关于asp.net-mvc - MVC : How to know which input value from HttpPostedFileBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151761/

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