界面代码(注意:runat=“Server”和input file中name一定要有)
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form action="../PMail/ListMail" runat="server" method="post" enctype="multipart/form-data"> <div> <input type="file" name="file" multiple/> <input type="submit" value="提交" /> </div> </form> </body> </html>
服务器代码
public ActionResult ListMail() { System.Web.HttpFileCollectionBase files = Request.Files; for (var i = 0; i < files.Count; i++) { HttpPostedFileBase postFile = files[i]; string fileName = System.IO.Path.GetFileName(postFile.FileName); if (!string.IsNullOrEmpty(fileName)) { string fileExtension = System.IO.Path.GetExtension(fileName); string directory = Server.MapPath("/MyFile/"); string path = directory + fileName; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } postFile.SaveAs(path); } }
} }