C# 从上载的多个文件中获取文件名
我的表格中有C# 从上载的多个文件中获取文件名,c#,asp.net-mvc,file-upload,C#,Asp.net Mvc,File Upload,我的表格中有4个文件上传。我想当我点击一个按钮,我得到的文件名和设置为模型,然后文件上传到服务器。但我不想必须使用4文件,我可以选择使用3,2,或1文件上传一次。如果我只使用2文件上传,file3和file4模型仍然为空。我必须在控制器中执行什么操作 有什么想法或建议吗 型号: public string file1 {get;set;} public string file2 {get;set;} public string file3 {get;set;} public string fil
4个文件上传
。我想当我点击一个按钮,我得到的文件名和设置为模型,然后文件上传到服务器。但我不想必须使用4文件,我可以选择使用3,2,或1文件上传一次。如果我只使用2文件上传,file3和file4模型仍然为空。我必须在控制器中执行什么操作
有什么想法或建议吗
型号:
public string file1 {get;set;}
public string file2 {get;set;}
public string file3 {get;set;}
public string file4 {get;set;}
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
<input type="file" name="files" id="file3" />
<input type="file" name="files" id="file4" />
<input type="submit" />
}
查看:
public string file1 {get;set;}
public string file2 {get;set;}
public string file3 {get;set;}
public string file4 {get;set;}
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
<input type="file" name="files" id="file3" />
<input type="file" name="files" id="file4" />
<input type="submit" />
}
@使用(Html.BeginForm(“action”、“controller”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
}
在您的视图中:
public string file1 {get;set;}
public string file2 {get;set;}
public string file3 {get;set;}
public string file4 {get;set;}
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
<input type="file" name="files" id="file3" />
<input type="file" name="files" id="file4" />
<input type="submit" />
}
注意:此处不需要您的型号名称。通过操作结果参数,您将传递文件
@使用(Html.BeginForm(“文件上传”、“主页”),
FormMethod.Post,新的{enctype=“multipart/form data”})
{
}
在控制器中:
public string file1 {get;set;}
public string file2 {get;set;}
public string file3 {get;set;}
public string file4 {get;set;}
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
<input type="file" name="files" id="file3" />
<input type="file" name="files" id="file4" />
<input type="submit" />
}
[HttpPost]
公共操作结果文件上载(HttpPostedFileBase文件1、HttpPostedFileBase文件2、HttpPostedFileBase文件3、HttpPostedFileBase文件4)//或IEnumerable文件
{
HttpPostedFileBase file1=Request.Files[“file1”];
HttpPostedFileBase file2=Request.Files[“file2”];
HttpPostedFileBase file3=Request.Files[“file3”];
HttpPostedFileBase file4=Request.Files[“file4”];
if(file1!=null)//与file2、file3、file4相同
{
//如果这是真的,那么file1有file。,
}
//检查并保存文件1//与文件2、文件3、文件4相同
如果(file1.ContentLength>0)
{
字符串filePath=Path.Combine(HttpContext.Server.MapPath(“../Uploads”),
GetFileName(uploadFile.FileName));
uploadFile.SaveAs(文件路径);
}
返回视图();
}
有一篇关于文件上传的文章
我没有检查上面的代码。如果这不起作用,请告诉我。您的控制器是什么样子的?@JustinHelgerson谢谢您的回复,它是唯一的模型和视图,我不知道我应该在控制器中做什么,有什么想法吗?您有没有看任何现有的问题@贾斯汀谢谢贾斯汀,我可以只使用一个文件上传,如何使用多个文件上传和使用IEnumerable这样。我无法获得文件名是的,它的工作,谢谢你,但它的工作如果不同名称的文件上传,你能给我一个相同名称的文件上传这样的例子是的,多个文件上传,但相同名称的文件上传和使用IEnumerable,我想如果使用它,我不需要像这样重复代码:
并保留操作结果公共操作结果文件上载(IEnumerable files){//Your Operations Here return View();}
@RJK这正是我需要的,谢谢您的明确指示。