[英]ASP.NET MVC Upload file
I'm a beginner in ASP.NET MVC and I'm trying to create my first web application.我是 ASP.NET MVC 的初学者,我正在尝试创建我的第一个 Web 应用程序。 Where I want users to be able to upload files.
我希望用户能够上传文件的地方。 But all I get is a 404 ERROR when I go to http://localhost:64679/File/Index .
但是当我转到http://localhost:64679/File/Index时,我得到的只是一个 404 错误。 I can't really see where the problem is:
我真的看不出问题出在哪里:
Index.cshtml:索引.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
}
Controller:控制器:
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();
}
}
}
You need to add Index controller to load the page as shown below.您需要添加索引控制器来加载页面,如下所示。
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Index()
{
return View();
}
Index.cshtml:索引.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
}
Controller:控制器:
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();
}
}
}
You're code is perfect just need to replace Controller name while post a form from .cshtml file.你的代码是完美的,只需要在从.cshtml文件发布表单时替换控制器名称。 So just replace that controller name with FileController you're issue will be resolved.
因此,只需将该控制器名称替换为FileController 即可解决您的问题。
Add one get method as well.还要添加一个 get 方法。 I already mentioned with same in example.
我已经在例子中提到过。
replace Html.BeginForm("Index"," Home ",FormMethod.Post with Html.BeginForm("Index"," File ",FormMethod.Post将Html.BeginForm("Index"," Home ",FormMethod.Post 替换为Html.BeginForm("Index"," File ",FormMethod.Post
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.