上传多个文件,上传有ID

本文关键字:ID 文件 | 更新日期: 2023-09-27 18:17:35

我有一个页面,其中有多个文件上传输入,其中ID和名称与正在上传的文档类型相关,看起来像:

<input type="file" name="postedFile_37" id="37">
<input type="file" name="postedFile_23" id="23">

在我的控制器中,我如何识别上传的名称或id,以便我可以将文档分配给在DB中上传的类型?

例如,我可以看到如果我输入
Request.Files[i]

我可以看到索引的名称,但我不能得到保存的值。我如何从上传的文件中获得名称或ID ?

上传多个文件,上传有ID

尝试在每个文件旁边添加隐藏字段,这样您就有两个数组-第一个是文件本身,第二个是id。

<input type="hidden" name="fileId" value="37" />
<input type="file" name="file" />
<input type="hidden" name="fileId" value="38" />
<input type="file" name="file" />

.

public ActionResult Test (string[] fileId, List<HttpPostedFileBase> file)
{
    int i = 0;
    foreach (var f in file)
    {
        var id = fileId[i]; // this is your file id, f is file
        i++;
    }
}

如果您使用ASP。. NET FileUpload控件而不是普通的HTML控件,您可以在PostBack中单独(通过ID)访问它们。