C# 上传文件并在操作中获取它';s控制器
我有一个上传文件的应用程序,我提出了以下观点:C# 上传文件并在操作中获取它';s控制器,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个上传文件的应用程序,我提出了以下观点: @{ ViewBag.Title = "Upload a projet"; } <section id="logout"> @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) { @Html.AntiForgeryToken() <
@{
ViewBag.Title = "Upload a projet";
}
<section id="logout">
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="@Url.Action("Logout", "Akeo")" >Se déconnecter</a>
}
</section>
@using (Html.BeginForm("Uploading_validation", "Akeo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="dossier" />
<br />
<input type="submit" value="OK" />
}
问题是,即使我选择了一个文件,参数file
也总是空的
这是什么原因?如何修改代码以修复此错误?尝试重命名操作中的参数
公共操作结果上载\u验证(HttpPostedFileBase档案)
尝试重命名操作中的参数公共操作结果上载\u验证(HttpPostedFileBase档案)
将输入类型的名称重命名为file,然后查看其工作方式是否如下
@{
ViewBag.Title = "Upload a projet";
}
<section id="logout">
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="@Url.Action("Logout", "Akeo")" >Se déconnecter</a>
}
</section>
@using (Html.BeginForm("Uploading_validation", "Akeo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<br />
<input type="submit" value="OK" />
}
@{
ViewBag.Title=“上传项目”;
}
@使用(Html.BeginForm(“注销”、“帐户”、FormMethod.Post、新{id=“logoutForm”})){
@Html.AntiForgeryToken()
}
@使用(Html.BeginForm(“上传_验证”,“Akeo”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
}
将输入类型的名称重命名为file,然后查看其工作方式是否如下
@{
ViewBag.Title = "Upload a projet";
}
<section id="logout">
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="@Url.Action("Logout", "Akeo")" >Se déconnecter</a>
}
</section>
@using (Html.BeginForm("Uploading_validation", "Akeo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<br />
<input type="submit" value="OK" />
}
@{
ViewBag.Title=“上传项目”;
}
@使用(Html.BeginForm(“注销”、“帐户”、FormMethod.Post、新{id=“logoutForm”})){
@Html.AntiForgeryToken()
}
@使用(Html.BeginForm(“上传_验证”,“Akeo”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
}
HttpPostedFileBase
实例。再次注意,参数名称与文件输入的名称匹配。检查HttpPostedFileBase
实例。再次注意,参数名称与文件输入的名称匹配。选中我将代码更改为“[HttpPost]公共操作结果上载”\u validation(){HttpPostedFileBase fileurl=null;foreach(Request.Files中的字符串文件){fileurl=Request.Files[file];}如果(fileurl!=null&&fileurl.ContentLength>0){var fileName=Path.GetFileName(fileurl.fileName);var path=path.Combine(Server.MapPath(@“C:\Inetpub\wwwroot\”,文件名);fileurl.SaveAs(path);}return RedirectToAction(“Index”);}`但新错误在路径中(它需要的是虚拟路径而不是物理路径)。这是什么意思?我不知道你为什么在路径上有错误。通常im使用AppDomain.CurrentDomain.BaseDirectory+“/mypathtosave”;这对我来说很有效,但我不知道这是不是正确的方法:)我将代码更改为“[HttpPost]公共操作结果上载\u验证(){HttpPostedFileBase fileurl=null;foreach(Request.Files中的字符串文件){fileurl=Request.Files[file];}如果(fileurl!=null&&fileurl.ContentLength>0){var fileName=Path.GetFileName(fileurl.fileName);var Path=Path.Combine(Server.MapPath(@“C:\Inetpub\wwwroot\”,fileName);fileurl.SaveAs(Path);}return RedirectToAction(“Index”);}`但新错误在路径中(它需要的是虚拟路径,而不是物理路径)。这是什么意思?我不知道为什么路径中有错误。通常我使用AppDomain.CurrentDomain.BaseDirectory+“/mypathtosave”;它对我有效,但我不知道是不是正确的方法:)请查看我的编辑现在的问题是我需要一个绝对路径而不是在服务器中您必须给出文件夹的虚拟路径而不是物理路径,因此输入path.Combine(Server.MapPath(“”),fileName);如果要将其上载到根目录,请在其中输入文件夹的名称作为Path.Combine(Server.MapPath(“upload”),fileName);请查看我的编辑现在的问题是我需要一个绝对路径,而不是服务器内部。您必须提供文件夹的虚拟路径,而不是物理路径,因此输入Path.Combine(Server.MapPath(“”),fileName);如果要将其上载到根目录,请在其中输入文件夹名称作为Path.Combine(Server.MapPath(“上载”),fileName);