asp.net mvc实现上传文件
最基本的实现方法:
视图:
<% Html.BeginForm("Upload", "UploadTest", FormMethod.Post, new { enctype = "multipart/form-data" });
%>
<input type="file" name="fileLoad1" />
<input type="submit" value="上传" />
<% Html.EndForm(); %>
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(FormCollection formCollection)
{
int cnt=Request.Files.Count;
if (cnt > 0)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(Request.Files["fileLoad1"].FileName);
Request.Files["fileLoad1"].SaveAs(Path.Combine(path, filename));
string savename = "upload/" + filename;//这是要向数据库表中保存的数据
}
return View();
}
网上牛人们还写了很多种方法,可以参考