您不使用文件输入控件。在ASP.NET
MVC中不使用服务器端控件。请查看以下博客文章,该文章说明了如何在ASP.NET MVC中实现此目的。
因此,您将从创建一个包含文件输入的HTML表单开始:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){ <input type="file" name="file" /> <input type="submit" value="OK" />}然后您将有一个控制器来处理上传:
public class HomeController : Controller{ // This action renders the form public ActionResult Index() { return View(); } // This action handles the form POST and the upload [HttpPost] public ActionResult Index(HttpPostedFilebase file) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract only the filename var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } // redirect back to the index action to show the form once again return RedirectToAction("Index"); }}


