[英]Uploading file using MVC4
這是我的代碼-
//in Profile.cshtml
@{
ViewBag.Title = "Profile";
}
<h2>Profile</h2>
@using (Html.BeginForm("Upload", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
在我的控制器(ProfileController.cs)中,我有-
namespace MvcApplication1.Controllers
{
public class ProfileController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Profile() {
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Media/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Profile");
}
}
}
但是每次我單擊“確定”上傳文件時,都會出現此錯誤-本地主機:12345 /配置文件/上傳-Internet Explorer無法顯示此網頁。
我的Profile控制器中有一個名為Upload的方法,該方法應該接受此文件。
我想念什么?
似乎您沒有處理POST動詞請求的操作方法。 動作方法的默認動詞(如果未指定)是GET。 當您的代碼示例中確實有一個操作方法時,[HttpPost]屬性將被注釋掉。 這將使該方法僅適用於GET請求-否則,您將獲得404(無法顯示頁面)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.