【问题标题】:ASP .NET RAZOR FileUploadASP .NET RAZOR 文件上传
【发布时间】:2013-11-11 15:16:56
【问题描述】:

我是 ASP.NET MVC RAZOR 的新手,我正在尝试将文件上传到我的页面。我发现了很多关于这个主题的问题,但我有一个错误,我不知道为什么。 这是我认为的表格:

    @using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    <input type="file" name="file" />
    <input type="submit" name="Submit" id="Submit" value="Upload" />   

}

这是我的控制器:

namespace Upload.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/

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

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);                    
                var path = Path.Combine("C:\\temp\\", fileName);
                file.SaveAs(path);                
            }
            return RedirectToAction("Index"); ;
        }
    }
}

当我运行我的页面时,我得到一个错误,它说: “未找到资源:“/上传”。 我的错误在哪里?抱歉,我知道我是 ASP.NET 的初学者,但我阅读了很多教程,只是希望它能够正常工作。 非常感谢。

【问题讨论】:

标签: .net asp.net-mvc razor


【解决方案1】:

您的控制器名为Upload,但您的操作也是如此。您必须使用 /Upload/Upload/ 作为 URL,或者将 Upload 操作更改为 Index,因为后者是默认操作。

【讨论】:

  • 感谢您的回答,我在家庭控制器中实现了上传方法,现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多