ASP.NET MVC上传文件

时间:2017-04-23 09:41:43

标签: asp.net-mvc

我是ASP.NET MVC的初学者,我正在尝试创建我的第一个Web应用程序。我希望用户能够上传文件。但是当我去http://localhost:64679/File/Index时,我得到的只是404错误。我无法真正看出问题出在哪里:

Index.cshtml:

<h2>Basic File Upload</h2>
@using (Html.BeginForm("Index","Home",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <label for="file">Upload Files:</label>
    <input type="file" name="file" id="file" /><br><br>
    <input type="submit" value="Upload File" />
    <br><br>
    @ViewBag.Message
}

控制器:

namespace FileUploadApplication.Controllers
{
    public class FileController : Controller
    {
        // GET: File
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Images"),
                                               Path.GetFileName(file.FileName));
                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }
            return View();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您需要添加索引控制器以加载页面,如下所示。

    public ActionResult Index()
    {
        return View();
    }

答案 1 :(得分:0)

[HttpGet]
public ActionResult Index()
{
    return View();
}

答案 2 :(得分:0)

Index.cshtml:

<h2>Basic File Upload</h2>
@using (Html.BeginForm("Index","File",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <label for="file">Upload Files:</label>
    <input type="file" name="file" id="file" /><br><br>
    <input type="submit" value="Upload File" />
    <br><br>
    @ViewBag.Message
}

控制器:

namespace FileUploadApplication.Controllers
    {
        public class FileController : Controller
        {

            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public ActionResult Index(HttpPostedFileBase file)
            {
                if (file != null && file.ContentLength > 0)
                    try
                    {
                        string path = Path.Combine(Server.MapPath("~/Images"),
                                                   Path.GetFileName(file.FileName));
                        file.SaveAs(path);
                        ViewBag.Message = "File uploaded successfully";
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "ERROR:" + ex.Message.ToString();
                    }
                else
                {
                    ViewBag.Message = "You have not specified a file.";
                }
                return View();
            }
        }
    }

您的代码是完美的,只需要在 .cshtml 文件中发布表单时替换控制器名称即可。因此,只需将控制器名称替换为 FileController 即可解决问题。

也添加一个get方法。我已经在示例中提到了。

Html.BeginForm(“ Index”,“ 文件替换 Html.BeginForm(” Index“,” 首页“,FormMethod.Post ”,FormMethod.Post