上传文件并在动作's控制器中获取

本文关键字:控制器 获取 文件 | 更新日期: 2023-09-27 18:15:56

我有一个上传文件的应用程序,我创建了这个视图:

@{
    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" />
 }

动作Uploading_validation

 [HttpPost]
        public ActionResult Uploading_validation()
    {
        HttpPostedFileBase fileurl = null; 
        foreach (string file in Request.Files)
        {
             fileurl = Request.Files[file];
        }
        // Verify that the user selected a file
        if (fileurl != null && fileurl.ContentLength > 0)
        {
            // extract only the fielname
            var fileName = Path.GetFileName(fileurl.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = @"C'Inetpub'wwwroot'"+fileName;
            fileurl.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }

的问题,即使我选择一个文件参数file总是空的!!

这是什么原因?如何修改代码来修复此错误?

上传文件并在动作's控制器中获取

将输入类型的名称重命名为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" />
}

尝试重命名public ActionResult Uploading_validation(HttpPostedFileBase dossier)