日期:2014-05-17 浏览次数:20487 次
@{ Layout = null; } <script type="text/javascript" src="../../Scripts/jquery-1.4.4.min.js"></script> <script type="text/javascript"> function uploadImage() { if ($("#uploadIMG").val().length < 1) { alert("Choisir une image."); return false; } else { $("#formtest").submit(); } } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Upload</title> </head> <body> <form enctype="multipart/form-data" method="post" id="formtest" action="uploadIMG/Upload"> <text>Choisir une image:</text><input type="file" name="fileIMG" id="uploadIMG" /><br /> <input type="button" id="btntest" value="Upload" onclick="uploadImage()"/> </form> <img src="/Image/@ViewBag.FileName" /> </body> </html>
public class UploadIMGController : Controller { // // GET: /UploadIMG/ public void Upload() { HttpPostedFileBase file = Request.Files["fileIMG"]; string nomstr=System.Guid.NewGuid().ToString().Substring(0, 6); if (file.ContentLength > 0) { file.SaveAs(Server.MapPath("~/Image/") + nomstr + System.IO.Path.GetExtension(file.FileName)); } //ViewBag.FileName = System.IO.Path.GetFileName(file.FileName); ViewBag.FileName = nomstr + System.IO.Path.GetExtension(file.FileName); } }