1
我試圖做我的第一個簡單的文件上傳在MVC 5.我下了一堆我發現的例子,但由於某種原因,在我的「創建」的ActionResult的uploadFile總是進來爲NULL,因此上傳代碼從不運行。任何人看到我可能做錯了什麼?簡單的文件上傳返回NULL?
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Documents.</h2>
<h4>Upload a new document.</h4>
<div class="well">
@using (Html.BeginForm("Create", "Documents", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<h3>Select a file to upload. </h3>
<input type="file" name="files" value="" multiple="multiple" />
<input type="submit" value="Upload your file" title="Upload" />
<div style="color:Red;font-size:14px">@ViewBag.Message</div>
}
</div>
這裏是我的控制器:
// POST: Documents/Create
[HttpPost]
public ActionResult Create(HttpPostedFileBase uploadFile)
{
try
{
if(uploadFile != null && uploadFile.ContentLength > 0)
{
string filePath = Server.MapPath("../SiteDocuments" + uploadFile.FileName);
uploadFile.SaveAs(filePath);
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
return View();
}
}
謝謝,就是這樣。一旦我獲得了匹配的元素名稱以及它在其中工作的.MapPath中缺少的斜槓, – Caverman
@Caverman:歡迎您! IMHO,嘗試使用'Path.Combine'而不是手動串聯,如圖此[柱](http://stackoverflow.com/a/26111460/40521) – Shyju
我最初有來自一個例子,我發現但我去回到簡單的使用Server.Mappath,看看是否能作任何形式的差異,這當然沒有。我會回到Path.Combine。 – Caverman