gpt4 book ai didi

c# - C# Request.Files[n].FileName 的哪个文件表单字段

转载 作者:行者123 更新时间:2023-11-28 04:56:32 25 4
gpt4 key购买 nike

如果我有一个包含多个输入文件字段的 HTML 表单(其中“N”是唯一编号);

<input type="file" name="inputFileN"> 

然后在C#代码中;

string inFile = System.IO.Path.GetFileName(Request.Files[M].FileName)

有什么方法可以从请求数据中确定“M”的值,以便我可以匹配特定的 HTML 输入文件类型字段?

这是在最终用户可以更新编辑表单上的字段的情况下,它适用于除文件类型字段之外的所有字段类型。

最佳答案

所有必要的数据都在 HttpContext.Request.Files 中;具体来说,在 HttpContext.Request.Files.AllKeys 中:

//HttpContext is a member of `System.Web.Mvc.Controller`,
//accessible in controllers that inherit from `System.Web.Mvc.Controller`.
System.Web.HttpFileCollectionBase files = HttpContext.Request.Files;
string[] fieldNames = files.AllKeys;
for (int i = 0; i < fieldNames.Length; ++i)
{
string field = fieldNames[i]; //The 'name' attribute of the html form
System.Web.HttpPostedFileBase file = files[i];
string fileName = files[i].FileName; //The path to the file on the client computer
int len = files[i].ContentLength; //The length of the file
string type = files[i].ContentType; //The file's MIME type
System.IO.Stream stream = files[i].InputStream; //The actual file data
}

关于c# - C# Request.Files[n].FileName 的哪个文件表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057680/

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