0

I simply tried this, but its not working, what is the problem in it,

MY index page:

 @{
 ViewBag.Title = "Index";
 }

  @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/from-    data" }))
    { 
       <div>
       <h1 style="align-content: center; color: blueviolet">Application to upload files</h1>
       </div>
       <div>
       <input type="file" id="file" name="file" />
       <br />
       <input type="submit" id="load" name="submit" value="Submit" />
       </div>

       }

And My controller is,

                  [HttpPost]
    public ActionResult Upload()
    {
        string path = @"~/Content/Upload";

        HttpPostedFileBase file = Request.Files["file"];

        if (file != null)
            file.SaveAs(path + file.FileName);

        return Content("Sucess");
    }
4

1 回答 1

1

您尝试将文件保存到的路径看起来错误。尝试使用 MapPath:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    string path = Server.MapPath("~/Content/Upload");
    if (file != null)
    {
        file.SaveAs(Path.Combine(path, file.FileName));
    }

    return Content("Sucess");
}

还要确保您enctype在表单中使用了正确的属性:

enctype = "multipart/form-data"

代替:

enctype = "multipart/from-    data"
于 2013-09-13T12:34:29.007 回答