0

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

@{
    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总是空的问题!

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

4

2 回答 2

2

将输入类型的名称重命名为文件,看看它是否像这样工作

@{
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" />
}
于 2013-05-29T10:59:26.470 回答
1

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

于 2013-05-29T10:20:03.867 回答