你的位置:首页 > ASP.net教程

[ASP.net教程]MVC上传(单文件)


后台代码:

 public ActionResult upload()    {      //获取文件对象      var file = Request.Files[0];      string suffix/*文件格式*/ = System.IO.Path.GetExtension(file.FileName);      string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + suffix;      string saveDic = "/upload/";      if (file != null)      {        if (!Directory.Exists(Server.MapPath(saveDic)))        {          Directory.CreateDirectory(Server.MapPath(saveDic));        }        file.SaveAs(Server.MapPath(saveDic) + fileName);                return Content("<script>alert('添加成功!')</script>");      }      else      {        return Content("0");      }    }

 前台代码:

<form action="/test/upload" enctype="multipart/form-data" method="post">  <input type="file" name="file" />  <button id="upload">确定</button></form>