我有一個簡單的情況,我有一個頁面上傳文件,一些導入。目前,我所擁有的只是在我的頁面上輸入文件。MVC文件上傳驗證
這是我的GET控制器看起來像
public ActionResult FileUpload()
{
return View();
}
這是我的觀點看起來像
@{
ViewBag.Title = "FileUpload";
}
<h2>FileUpload</h2>
<form action="/Home/FileUpload" method="post" enctype="multipart/form-data">
<input type="file" id="newFile" name="newFile" />
<input type="submit" id="submitButton" value="Submit" />
</form>
,這就是我的職務行爲看起來像
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase newFile)
{
if (newFile.ContentLength > 0)
{
//do stuff here
}
return View("Index");
}
你當然會注意到這裏沒有提到模型,因爲我找不到爲這種情況創建模型的方法灰。我想要進行一些非常基本的驗證,比如'請在上傳之前選擇一個文件',這就是所有。
有沒有辦法實現這個?
在此先感謝
將